在Spring中有三种装配方式
省略引用类型ref
<bean id="cat" class="com.yang.entity.Cat"/>
<bean id="dog" class="com.yang.entity.Dog"/>
<!--
byName:会在容器上下文中查找,和自己对象set方法后面的值相对应的beanid
byType:会自动在容器上下文中查找,和自己对象属性类型相同的bean
-->
<bean id="people" class="com.yang.entity.People" autowire="byName">
<property name="name" value="yang"/>
</bean>
<bean id="people1" class="com.yang.entity.People" autowire="byType">
<property name="name" value="yang"/>
</bean>
</beans>
小结:
byName的时候,需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致
byType的时候,需要保证所有bean的class唯一,并且这个bean需要和自动注入的属性的类型一致
各有弊端
jdk1.5支持的注解 Spring2.5支持的注解
The introduction of annotation-based configuration raised the question of whether this approach is “better” than XML
使用注解须知:
1.导入约束:context约束
xmlns:context=“http://www.springframework.org/schema/context”
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
2.配置注解的支持context:annotation-config
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--开启注解支持-->
<context:annotation-config/>
</beans>
@Autowired
直接在属性上使用即可!也可以在set方式上使用
使用Autowired我们可以不用编写set方法了,前提是你这个自动装配的属性在IOC(Spring)容器中存在,且符合名字byType
默认是byType,有相同类型字段时byName
@Nullable 字段标记了这个注解,说明这个字段可以为null;
@Resource注解,不指定name值,先去判断byName和byType,有一个能注入即成功
小结:@Resource和@Autowired的区别
都是用来自动装配的,都可以放在属性字段上
@Autowired通过byType的方式实现,而且必须要求这个对象存在!
@Resource默认通过byName的方式实现,如果找不到名字,则通过byType实现!如果两个都找不到的情况下,就报错!
执行顺序不同:@Autowired通过byType的方式实现。@Resource默认通过byName的方式实现。
在Spring4之后,要使用注解开发,必须保证aop的包导入了
使用注解需要导入context约束,增加注解的支持!
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--开启注解支持-->
<context:annotation-config/>
<!--指定要扫描的包,这个包下的注解就会生效-->
<context:component-scan base-package="com.wen"/>
</beans>
1. bean注入使用@Componet注解
@Component 组件,放在类上说明这个类被spring管理了,就是一个bean
2. 属性注入使用@Value注解
3、衍生注解
@Componet有几个衍生注解,我们在web开发中,会按照mvc三层架构分层!
dao层 【@Repository】
service层 【@Service】
controller层 【@Controller】
这四个注解功能都是一样的,都是代表将某个类注册到Spring中,装配Bean
4、自动装配
@Autowired 自动装配通过类型、名字
如果Autowired不能唯一自动装配上属性,则需要通过@Qualifier(value="xxx")
@Nullable 字段标记了这个注解,说明这个字段可以为null
@Resource 自动装配通过名字,类型
5. 作用域
@Scope(“singleton”)单例
6. 小结
XML 与 注解
完全脱离xml,实质还是使用注解
JavaConfig时spring的一个子项目,在spring4之后,成为了一个核心功能
实体类:
@Component
public class User {
private String name;
public String getName() {
return name;
}
@Value("wen")
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name=‘" + name + ‘\‘‘ +
‘}‘;
}
}
配置类:
// 配置类
@Configuration
@ComponentScan("com.wen.pojo")
public class SpringConfig {
@Bean
public User getUser() {
return new User();
}
}
测试类:
不加@Component就是单例
spring(五)bean的自动装配及纯Java配置spring
原文:https://www.cnblogs.com/wendaidai/p/15161302.html