sping学习(1):通过注解装配Bean

    xiaoxiao2023-10-18  174

    文章目录

    1.通过注解装配Bean1.1使用@Component装配Bean1.2自动装配 --- @Autowried1.3自动装配的歧义性(@Primary 和 @Qualifier)1.3.1注解@Primary1.3.2注解\@Qualifier 1.4.装载带有参数的构造方法类1.5.使用@Bean装配Bean 2.@Autowired 、@Resource 、@Qualifier的区别2.1@Autowired 与@Resource的区别:

    1.通过注解装配Bean

    1.1使用@Component装配Bean

    注解@Component代表Spring Ioc会把这个类扫描生成Bean实例,其中value属性代表这个类在Sping中的id;

    注解@Value代表的是值的注入;

    注解@component只能注解在类上,不能注解在方法上;

    package com.ssm.chapter10.annotation.pojo; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component(value = "role") public class Role { @Value("1") private Long id; @Value("role_name_1") private String roleName; @Value("role_note_1") private String note; /*********** setter and getter ************/ }

    1.2自动装配 — @Autowried

    注解@Autowired 根据类型去寻找定义的Bean然后将其注入;如果寻找不到或找到多个,则抛异常;

    @Autowired的配置项required: ①默认为true,即一定要找到对应类型的bean,否则抛异常; ②@Autowired(required = false) 如果找不到对应类型的bean,则允许不注入,不会抛异常,但是该字段为空;

    @Autowired除可以配置在属性之外,还允许方法配置,常见的Bean的setter方法也可以使用它来完成注入。

    package com.ssm.chapter10.annotation.service.impl; /*********************imports***********************/ @Component("RoleService2") public class RoleServiceImpl2 implements RoleService2 { private Role role = null; public Role getRole() { return role; } @Autowired public void setRole(Role role) { this.role = role; } .............. }

    1.3自动装配的歧义性(@Primary 和 @Qualifier)

    采用按类型的方式自动装配对象,会产生歧义性:在Java中接口可以有多个实现类,同样的抽象类也可以用多个实例化的类,这样就会造成通过类型(by type)获取Bean的不唯一,从而导致Spring IOC类型于按类型的方法无法获取到唯一的实例化类。

    1.3.1注解@Primary

    注解@Primary标志在具体的实现类上,用于告诉Sping IOC容器,当存在多个实现类时,优先注入使用@primary标注的实现类。

    如果同一接口的实现类,存在两个或多个由@Primary标注,则注入时会抛异常;

    1.3.2注解@Qualifier

    @Qualifier标注在引用类的属性上,按名称注入,示例如下:

    @Autowired @Qualifier(“roleService3”) private RoleService roleService = null;

    package com.ssm.chapter10.annotation.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; import com.ssm.chapter10.annotation.pojo.Role; import com.ssm.chapter10.annotation.service.RoleService; @Component public class RoleController { @Autowired @Qualifier("roleService3") private RoleService roleService = null; public void printRole(Role role) { roleService.printRoleInfo(role); } }

    1.4.装载带有参数的构造方法类

    带参数的构造方法,可以使用@Autowired 或 @Qualifier进行参数注入:

    @Component public class RoleController2 { private RoleService roleService = null; public RoleController2(@Autowired @Qualifier("roleService3") RoleService roleService) { this.roleService = roleService; } /*********** setter and getter ************/ }

    或者:

    public RoleController2(@Autowired RoleService roleService) { this.roleService = roleService; }

    1.5.使用@Bean装配Bean

    注解@Bean,可以注解到方法上,并且将方法返回的对象作为Spring的Bean,存放在IOC容器中。

    @Bean(name = "dataSource") public DataSource getDataSource() { Properties props = new Properties(); props.setProperty("driver", "com.mysql.jdbc.Driver"); props.setProperty("url", "jdbc:mysql://localhost:3306/chapter10"); props.setProperty("username", "root"); props.setProperty("password", "123456"); DataSource dataSource = null; try { dataSource = BasicDataSourceFactory.createDataSource(props); } catch (Exception e) { e.printStackTrace(); } return dataSource; }

    2.@Autowired 、@Resource 、@Qualifier的区别

    spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource、@PostConstruct以及@PreDestroy。

    @Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按byName自动注入罢了。

    @Resource有两个属性是比较重要的,分是name和type,Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。

    @Resource装配顺序:

    1. 如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常;   2. 如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常;   3. 如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常;   4. 如果既没有指定name,又没有指定type,则自动按照byName方式进行装配;如果没有匹配,则回退为一个原始类型进行匹配,如果匹配则自动装配;

    2.1@Autowired 与@Resource的区别:

    1、 @Autowired与@Resource都可以用来装配bean,都可以写在字段上,或写在setter方法上。

    2、 @Autowired默认按类型装配(这个注解是属于spring的),默认情况下必须要求依赖对象必须存在:(1)如果要允许null值,可以设置它的required属性为false,如:@Autowired(required=false) ;(2)如果我们想使用名称装配可以结合@Qualifier注解进行使用,如下:

    @Autowired @Qualifier("userServiceImpl") public IUserService userService;

    或者:

    @Autowired public void setUserDao(@Qualifier("userDao") UserDao userDao) { this.userDao = userDao; }

    这样Spring会找到id为userServiceImpl和userDao的bean进行装配。

    @Resource(这个注解属于J2EE的),默认按照名称进行装配,名称可以通过name属性进行指定,如果没有指定name属性,当注解写在字段上时,默认取字段名进行安装名称查找,如果注解写在setter方法上默认取属性名进行装配。当找不到与名称匹配的bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。

    推荐使用:@Resource注解在字段上,这样就不用写setter方法了,并且这个注解是属于J2EE的,减少了与spring的耦合。这样代码看起就比较优雅。

    参考文章: @Autowired @Resource @Qualifier的区别 - 小虾米的java梦 - 博客园 https://www.cnblogs.com/fengli9998/p/7472247.html

    最新回复(0)