目录
1.Bean的自动装配
1.1? autowire="byName" 实现自动装配
1.2? autowire="byType" 实现自动装配
?2.注解实现自动装配
2.1 配置注解
2.2 @Autowired注解
2.3? @Resource注解
2.3小结
3.介绍一个idea中做笔记的小技巧
?
在Spring中有三种装配的方式:
- 在xml中显示的配置
- 在java中显示配置
- 隐式的自动装配bean
1.Bean的自动装配
自动装配是Spring满足bean依赖的一种方式,Spring会在上下文中自动寻找,并自动给bean装配属性。
1.1? autowire="byName" 实现自动装配
byname会自动在容器上下文中查找,和自己对象set方法后面的值对应的bean id。
需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致。
People.java
package org.example;
public class People {
private Cat cat;
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 + '\'' +
'}';
}
}
cat.java
package org.example;
public class Cat {
public void shut(){
System.out.println("喵喵喵……");
}
}
Dog.java
package org.example;
public class Dog {
public void shut(){
System.out.println("汪汪汪……");
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="cat" class="org.example.Cat"></bean>
<bean id="dog" class="org.example.Dog"></bean>
<bean id="people" class="org.example.People" autowire="byName">
<property name="name" value="小狂神"></property>
</bean>
</beans>
测试类
package org.example;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main( String[] args ) {
//获取ApplicationContext对象
ApplicationContext application=new ClassPathXmlApplicationContext("ApplicationContext.xml");
//通过ApplicationContext获得TestHello对象
//getBean()方法中的参数即为配置文件中Bean的id的值
People people=(People) application.getBean("people");
people.getCat().shut();
people.getDog().shut();
}
}
1.2? autowire="byType" 实现自动装配
byType:会自动在容器上下文中查找,和自己对象属性类型相同的bean。
需要保证所有bean的class唯一,并且这个bean需要和自动注入的属性的类型一致。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="cat" class="org.example.Cat"></bean>
<bean id="dog" class="org.example.Dog"></bean>
<bean id="people" class="org.example.People" autowire="byType">
<property name="name" value="小狂神"></property>
</bean>
</beans>
?2.注解实现自动装配
JDK1.5支持的注解,Spring2.5就支持注解了。
2.1 配置注解
只需在applicationContext.xml文件中加入
<?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:util="http://www.springframework.org/schema/util"
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/util https://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--开启注解的支持-->
<context:component-scan base-package="org.example"/>
</beans>
2.2 @Autowired注解
直接在属性上使用即可,也可以在set方式上使用。使用@Autowired 可以不用编写set方法了,前提是你这个自动装配的属性在IOC(Spring)容器中存在,且符合byname。
People.java
package org.example;
import org.springframework.beans.factory.annotation.Autowired;
public class People {
@Autowired
private Cat cat;
@Autowired
private Dog dog;
private String name;
public Cat getCat() {
return cat;
}
//set方法可以省略
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 + '\'' +
'}';
}
}
applicationContext.xml
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<bean id="cat" class="org.example.Cat"></bean>
<bean id="dog" class="org.example.Dog"></bean>
<bean id="people" class="org.example.People" ></bean>
<context:component-scan base-package="org.example"/>
</beans>
2.3? @Resource注解
People.java
package org.example;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.Resource;
public class People {
//如果没有(name="cat")那么就会找不到
@Resource(name = "cat2")
private Cat cat;
@Resource
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 + '\'' +
'}';
}
}
applicationContext.xml
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<bean id="cat1" class="org.example.Cat"></bean>
<bean id="cat2" class="org.example.Cat"></bean>
<bean id="dog" class="org.example.Dog"></bean>
<bean id="people" class="org.example.People" ></bean>
<context:component-scan base-package="org.example"/>
</beans>
2.3小结
@Autowired和@Resource的区别:
- 都是用来自动装配的,都可以放在属性字段上
- @Autowired通过byType的方式实现,而且必须要求这个对象存在
- @Resource默认通过byname的方式实现,如果找不到名字,则通过byType实现。如果两个都找不到的情况就会报错。
- 执行顺序不同:@Autowired通过byType;@Resource默认通过byname的方式实现。
3.介绍一个idea中做笔记的小技巧
?
?
?
|