Python 官方文档:入门教程 => 点击学习
目录在spring中有三种装配的方式:1.Bean的自动装配1.1 autowire="byName" 实现自动装配1.2 autowire="byType" 实现自动装配2.注解实
自动装配是Spring满足bean依赖的一种方式,Spring会在上下文中自动寻找,并自动给bean装配属性。
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();
}
}
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>
jdk1.5支持的注解,Spring2.5就支持注解了。
只需在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>
直接在属性上使用即可,也可以在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>
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>
@Autowired和@Resource的区别:
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注编程网的更多内容!
--结束END--
本文标题: 一篇文章教带你了解Java Spring之自动装配
本文链接: https://www.lsjlt.com/news/136115.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0