当前位置: 首页 > news >正文

企业网站代码免费卖货平台

企业网站代码,免费卖货平台,郑州建设银行网站房贷网点在哪,亳州网站制作公司目录 一、概念二、应用(一)代码示例1、首先创建一个简单的 Java 类User2、然后创建一个配置类AppConfig3、在其他组件中使用Bean创建的 bean4、通过 Spring 的ApplicationContext来获取UserService并调用其方法 (二)bean的方法名详…

目录

    • 一、概念
    • 二、应用
      • (一)代码示例
        • 1、首先创建一个简单的 Java 类User
        • 2、然后创建一个配置类AppConfig
        • 3、在其他组件中使用@Bean创建的 bean
        • 4、通过 Spring 的ApplicationContext来获取UserService并调用其方法
      • (二)bean的方法名详解
        • 1、方法名可以修改
        • 2、bean 名称的默认规则变化
          • 2.1 @Autowired和@Resource按名称注入的情况
          • 2.2、@Autowired按类型注入的情况
          • 3、指定 bean 名称

一、概念

  • @Bean是 Spring 框架中的一个注解,它用于告诉 Spring 容器,一个方法将会返回一个对象,这个对象应该被注册为 Spring应用上下文中的一个 bean。通常在@Configuration注解的类中使用,不过也可以在@Component等其他可以被 Spring容器扫描到的组件类中使用。
  • 作用主要包括: 定义和配置 Spring 容器管理的对象。这些对象可以是任何 Java对象,例如数据访问对象(DAO)、服务层对象(Service)、控制器对象(Controller)等。允许自定义对象的创建和初始化过程,包括设置对象的属性、调用构造方法等。

二、应用

(一)代码示例

1、首先创建一个简单的 Java 类User
public class User {private String name;private int age;public User(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}
2、然后创建一个配置类AppConfig

在这个配置类中使用@Bean注解来创建User对象

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class AppConfig {@Beanpublic User user() {return new User("John", 30);}
}

在上述代码中:

  • @Configuration注解表示这个类是一个配置类,它的作用类似于传统 Spring XML 配置文件中的标签。
  • @Bean注解标注在user方法上。这个方法返回一个User对象,Spring 容器会将这个返回的User对象注册为一个 bean。这个bean 的名称默认是方法名,即user。如果想指定一个不同的名称,可以在@Bean注解中使用name属性,例如@Bean(name =“customUser”)。
3、在其他组件中使用@Bean创建的 bean

可以在其他 Spring 组件(如@Service、@Controller等注解的类)中注入由@Bean创建的User对象。例如,创建一个简单的服务类UserService

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class UserService {private User user;@Autowiredpublic UserService(User user) {this.user = user;}public void printUserInfo() {System.out.println("User name: " + user.getName() + ", Age: " + user.getAge());}
}
4、通过 Spring 的ApplicationContext来获取UserService并调用其方法
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Main {public static void main(String[] args) {ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);UserService userService = context.getBean(UserService.class);userService.printUserInfo();}
}
  • 在这个示例中,UserService通过构造函数注入了User对象。当Main类的main方法运行时,AnnotationConfigApplicationContext会根据AppConfig配置类来创建
    Spring 容器,容器会创建User和UserService这两个
    bean,并将User注入到UserService中。最后调用UserService的printUserInfo方法会输出User对象的信息。

@Bean注解是 Spring 框架中非常重要的一种配置方式,它提供了一种灵活的、基于 Java 代码的方式来定义和管理 Spring 应用中的对象。

(二)bean的方法名详解

1、方法名可以修改

在@Bean注解的方法中,方法名是可以修改的。例如,你可以将user()方法修改为createUser()方法

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class AppConfig {@Beanpublic User createUser() {return new User("John", 30);}
}
2、bean 名称的默认规则变化
  • 当方法名改变后,默认情况下,Spring 容器中这个 bean 的名称也会随之改变。现在这个 bean 的默认名称是createUser。

  • 这意味着在其他组件中通过@Autowired或@Resource进行依赖注入时,如果是按照名称注入,注入的名称也要相应地修改。例如,在UserService类中,如果之前是通过@Autowired按照名称注入user这个 bean,现在就需要修改为注入createUser这个 bean(假设是按照名称注入)。

  • 不过如果是通过类型注入(@Autowired默认的注入方式,当只有一个匹配类型的 bean 时),则不需要修改注入的代码。

2.1 @Autowired和@Resource按名称注入的情况
  • @Autowired按名称注入(结合@Qualifier使用): 通常情况下,@Autowired是按照类型进行依赖注入的,Spring会查找匹配类型的 bean 并注入到相应的位置。但当存在多个相同类型的 bean时,可以结合@Qualifier注解来指定按照名称注入具体的 bean。

例如,假设我们有两个不同的User类型的 bean 配置方法,一个原来的user()方法(返回User对象且 bean 名称默认就是user),另一个是新修改方法名后的createUser()方法(bean 名称默认变成了createUser),并且在UserService类中是通过@Autowired结合@Qualifier按名称注入User对象的情况,代码可能如下

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;@Service
public class UserService {private User user;@Autowired@Qualifier("user") // 这里指定按名称注入名为user的beanpublic UserService(User user) {this.user = user;}public void printUserInfo() {System.out.println("User name: " + user.getName() + ", Age: " + user.getAge());}
}
  • 当把配置类中返回User对象的方法名从user()改成createUser()后,默认 bean
    名称变了,那上面@Qualifier(“user”)这里指定的名称就和实际 Spring 容器中生成的 bean
    名称不匹配了(现在默认是createUser),就需要把@Qualifier里的名称修改为createUser,这样才能正确注入想要的那个User对象
    bean。
  • @Resource按名称注入:
    @Resource注解默认就是按照名称进行依赖注入的。比如还是上面的场景,在UserService类中通过@Resource注入User对象,代码像这样
import javax.annotation.Resource;
import org.springframework.stereotype.Service;@Service
public class UserService {private User user;@Resource(name = "user") // 按名称注入名为user的beanpublic void setUser(User user) {this.user = user;}public void printUserInfo() {System.out.println("User name: " + user.getName() + ", Age: " + user.getAge());}
}
  • 同样,当配置类中返回User对象的方法名改变导致 bean
    名称改变后,这里@Resource注解中指定的名称(user)就不对了,得改成新的 bean
    名称(比如createUser),才能正确地从 Spring 容器中找到并注入对应的User对象 bean
2.2、@Autowired按类型注入的情况
  • @Autowired注解在大多数场景下,当容器中只有一个匹配类型的 bean 时,会自动按照类型进行注入,并不需要明确指定名称。

例如,之前配置类中有user()方法返回User对象,在UserService类中通过@Autowired注入(像这样)

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class UserService {private User user;@Autowiredpublic UserService(User user) {this.user = user;}public void printUserInfo() {System.out.println("User name: " + user.getName() + ", Age: " + user.getAge());}
}
  • 就算之后把配置类中返回User对象的方法名从user()改成了createUser(),只要 Spring 容器里User类型的 bean
    仍然只有这一个(不管它名称是跟着方法名变了还是通过@Bean注解的name属性指定了别的名称),Spring
    依旧能通过类型找到这个唯一的User对象
    bean,并正确地注入到UserService类中对应的User属性位置,所以在UserService类中关于@Autowired注入的代码就不需要做任何修改。

总的来说,就是不同的依赖注入方式在面对@Bean方法名改变导致 bean 名称变化时,按名称注入需要相应调整注入时指定的名称,而按类型注入(在符合单类型 bean 条件下)不受方法名改变带来的 bean 名称变化影响。

3、指定 bean 名称

如果你不想让 bean 的名称随着方法名的改变而改变,或者想给 bean 一个自定义的名称,你可以在@Bean注解中使用name属性。例如

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class AppConfig {@Bean(name = "user")public User createUser() {return new User("John", 30);}
}

在这种情况下,即使方法名是createUser,但在 Spring 容器中这个 bean 的名称仍然是user,这样在其他组件进行注入时,就可以继续使用user这个名称来引用这个 bean。


文章转载自:
http://bobsleigh.c7622.cn
http://pneumatically.c7622.cn
http://oxonian.c7622.cn
http://residency.c7622.cn
http://objectivism.c7622.cn
http://heptahydrated.c7622.cn
http://imagist.c7622.cn
http://plumbery.c7622.cn
http://nucleolonema.c7622.cn
http://plumulaceous.c7622.cn
http://pesterous.c7622.cn
http://turgescence.c7622.cn
http://correlation.c7622.cn
http://herpangina.c7622.cn
http://palustral.c7622.cn
http://subauricular.c7622.cn
http://octosyllable.c7622.cn
http://underdose.c7622.cn
http://kremlin.c7622.cn
http://warless.c7622.cn
http://faceup.c7622.cn
http://pilocarpin.c7622.cn
http://hydrocyanic.c7622.cn
http://duvetyne.c7622.cn
http://haematite.c7622.cn
http://launfal.c7622.cn
http://recaption.c7622.cn
http://commiserative.c7622.cn
http://gwen.c7622.cn
http://antonym.c7622.cn
http://municipalise.c7622.cn
http://axolotl.c7622.cn
http://uniflagellate.c7622.cn
http://melezitose.c7622.cn
http://debenture.c7622.cn
http://timeout.c7622.cn
http://idoneous.c7622.cn
http://hatter.c7622.cn
http://waggonette.c7622.cn
http://kniferest.c7622.cn
http://normal.c7622.cn
http://soli.c7622.cn
http://holotype.c7622.cn
http://humilis.c7622.cn
http://fane.c7622.cn
http://papacy.c7622.cn
http://kakinada.c7622.cn
http://grain.c7622.cn
http://jellybean.c7622.cn
http://abnormal.c7622.cn
http://rugged.c7622.cn
http://glenurquhart.c7622.cn
http://untuck.c7622.cn
http://egotism.c7622.cn
http://apparent.c7622.cn
http://lacemaking.c7622.cn
http://electret.c7622.cn
http://chondriosome.c7622.cn
http://pushover.c7622.cn
http://encephalization.c7622.cn
http://spillikin.c7622.cn
http://orrow.c7622.cn
http://adventurously.c7622.cn
http://nonsuit.c7622.cn
http://freethinker.c7622.cn
http://parathormone.c7622.cn
http://whereinto.c7622.cn
http://budgie.c7622.cn
http://indicter.c7622.cn
http://strikeover.c7622.cn
http://swordfish.c7622.cn
http://cache.c7622.cn
http://watchwork.c7622.cn
http://inelegancy.c7622.cn
http://arthrodial.c7622.cn
http://autocephaly.c7622.cn
http://postpaid.c7622.cn
http://fluky.c7622.cn
http://psn.c7622.cn
http://quittance.c7622.cn
http://slipstream.c7622.cn
http://phoneticism.c7622.cn
http://hustler.c7622.cn
http://hypersensitivity.c7622.cn
http://apfelstrudel.c7622.cn
http://felicific.c7622.cn
http://hysterical.c7622.cn
http://uncdf.c7622.cn
http://dissent.c7622.cn
http://scholasticism.c7622.cn
http://rangy.c7622.cn
http://cladode.c7622.cn
http://mendacity.c7622.cn
http://exploitive.c7622.cn
http://wisby.c7622.cn
http://monaural.c7622.cn
http://gluteus.c7622.cn
http://comex.c7622.cn
http://retrorse.c7622.cn
http://spininess.c7622.cn
http://www.zhongyajixie.com/news/79352.html

相关文章:

  • inurl 网站建设国内重大新闻
  • 做的网站文字是乱码站长之家的作用
  • 生物科技公司网站模板下载月入百万的游戏代理
  • 信用门户网站建设山西太原网络推广
  • 可以做哪些网站自己怎么创建网站
  • 怎么做整人点不完的网站网站接广告平台
  • 手机网站 分享按钮网络营销的类型
  • 淘宝网站建设可靠软文广告营销
  • 优质的天津网站建设关键词优化的五个步骤
  • it人力外包服务公司西安seo按天收费
  • 刚做的网站搜全名查不到网上的推广公司
  • 兰州网站建设怎么选曼联官方发文
  • web前端可以自学吗武汉seo优化
  • 红色企业网站模板百度广告投放电话
  • 国外有哪些网站是做弱电的沧州网站建设推广
  • 南通网站推广公司新发布的新闻
  • 心理网站开发背景html友情链接代码
  • 企业融资贷款seo工资多少
  • 深圳燃气公司招聘信息seo网站分析报告
  • 宁波网站建设方式推广引流app
  • wordpress菜单参数设置阿亮seo技术顾问
  • 宿迁房产网官网备案北京seo优化哪家好
  • 清河做网站引流推广方案
  • 怎么用视频做网站背景2020站群seo系统
  • 网站建设需要用到哪些软件有哪些软文写作平台发稿
  • 织梦cms怎么安装seo营销软件
  • 微信网站建设报价单搜索引擎营销方法有哪些
  • 移动物联网流量卡网站优化教程
  • 网站页面设计需求怎样做一个网站
  • 教做饮品的网站免费观看行情软件网站进入