使用Spring注解前的配置
<?xml version="1.0" encoding="UTF8"?>
<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" //需要添加的约束 名称空间
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context //对应约束声明的实列名称空间
http://www.springframework.org/schema/context/spring-context-3.2.xsd"> //
<!-- 指定要扫描的包,这个包下的注解就会生效 -->
<context:component-scan base-package="com.zxy"/>
<!-- 开启注解自动装配的支持 -->
<context:annotation-config/>
</beans>
1.自动装配
@Autowired
@Qualifier(value="xxx)
<!-- 开启注解自动装配的支持
需要在对应实体类下的属性上加入 @Autowired 通过byType 如果不能装配上唯一属性
@Qualifier(value="xxx) 配合 @Autowired使用 可以指定一个唯一的bean对象注入--!>
@Nullable @Nullable 被这个注释标记的字段 可以为null 例如:void setCat(@Nullable Cat cat
@Resource @Resource 默认为byName,找不到的话为 byType
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
public class People {
@Autowired
private Cat cat;
@Qualifier(value = "dog")
@Autowired
private Dog dog;
private String name;
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "People{" +
"cat=" + cat +
", dog=" + dog +
", name='" + name + '\'' +
'}';
}
}
<?xml version="1.0" encoding="UTF8"?>
<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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 开启注解自动装配的支持
需要在对应实体类下的属性上加入 @Autowired 通过byType 如果不能装配上唯一属性
@Qualifier(value="xxx) 配合 @Autowired使用 可以指定一个唯一的bean对象注入
-->
<context:annotation-config/>
<bean id="cat" class="com.zxy.pojo.Cat"/>
<bean id="dog" class="com.zxy.pojo.Dog"/>
<bean id="people" class="com.zxy.pojo.People"/>
<!-- @Nullable 被这个注释标记的字段 可以为null 例如:void setCat(@Nullable Cat cat) -->
<!-- @Resource 默认为byName,找不到的话为 byType -->
</beans>
2.衍生注解
@Component的衍生注解(类顶上)
pojo: @Component
dao: @Repository
service @Service
controller @Controller
下面三个和Component一样的,将某个类注册到Spring中,装配Bean 就无需再xml配置bean
补充:@Value 属性的注入
@Component
public class User {
@Value("zxyzxy") //相当与 <property name="name" value="zxyzxy"/>
public String name;}
//等价于 <bean id="user" class="com.zxy.pojo.User"/>
@Component //组件 说明被spring管理了,就是bean
public class User {
@Value("zxyzxy") //相当与 <property name="name" value="zxyzxy"/>
public String name;
}
@Service
public class UserService {
}
@Controller
public class UserController {
}
@Repository
public class UserDao {
}
4.作用域
@Scope("prototype")(类顶上) 默认为单例 多例prototype