(2).原型模式(每次从容器中get的时候,都会产生一个对象)
配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" 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="user2" class="Com.Jsxs.pojo.User" c:name="吉士先生" c:age="28" scope="prototype"/> </beans>
测试
@Test public void test(){ ApplicationContext context = new ClassPathXmlApplicationContext("userBeans.xml"); User user = context.getBean("user2",User.class); User user1 = context.getBean("user2",User.class); System.out.println(user==user1); }
(3).其他的模式
其他的模式在web应用开发中才会遇到。
(七)、Bean的自动装配
- 自动装配是Spring满足bean依赖的一种方式。
- Spring 会在上下文中自动寻找,并且给bean装配属性
1.在Spring 中有三种装配的方式
- 在XML中进行显示的配置 (章节七之前都是显示)
- 在Java中进行显示的配置 (还没接触)
- 隐式的自动装配bean (目前正在接触)
2.环境搭配
- 一个人有两只宠物
实体类
package Com.Jsxs.pojo; public class Cat { public void shout(){ System.out.println("喵喵喵喵喵..."); } }
package Com.Jsxs.pojo; public class Dog { public void shout(){ System.out.println("汪汪汪..."); } }
package Com.Jsxs.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class People { private Cat cat; private Dog dog; private String name; }
配置
<?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 --> <bean id="dog" class="Com.Jsxs.pojo.Dog"/> <bean id="cat" class="Com.Jsxs.pojo.Cat"/> <bean id="people" class="Com.Jsxs.pojo.People"> <property name="name" value="川川"/> <property name="cat" ref="cat"/> <property name="dog" ref="dog"/> </bean> </beans>
测试
import Com.Jsxs.pojo.People; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); People people = context.getBean("people", People.class); System.out.println(people); people.getDog().shout(); people.getCat().shout(); } }
3.byName (根据id)
byName :会自动在容器上下文进行查找,和自己对象set方法后面的值对应的bean 的id 相等的话,那么就可以使用
eg: setName-----对应的是set后面的,而不是属性名----> name,
(1).正列
实体类
package Com.Jsxs.pojo; public class Cat { public void shout(){ System.out.println("喵喵喵喵喵..."); } }
package Com.Jsxs.pojo; public class Dog { public void shout(){ System.out.println("汪汪汪..."); } }
package Com.Jsxs.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class People { private Cat cat; private Dog dog; private String name; }
配置
<?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 --> <bean id="dog" class="Com.Jsxs.pojo.Dog"/> <bean id="cat" class="Com.Jsxs.pojo.Cat"/> <!-- byName :会自动在容器上下文进行查找,和自己对象set方法后面的值对应的bean 的id 相等的话,那么就可以使用 --> <bean id="people" class="Com.Jsxs.pojo.People" autowire="byName"> <property name="name" value="川川"/> <!-- <property name="cat" ref="cat"/>--> <!-- <property name="dog" ref="dog"/>--> </bean> </beans>
(2).反列
配置
<?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 --> <bean id="dog" class="Com.Jsxs.pojo.Dog"/> <bean id="cat22" class="Com.Jsxs.pojo.Cat"/> <!-- byName :会自动在容器上下文进行查找,和自己对象set方法后面的值对应的bean 的id 相等的话,那么就可以使用 --> <bean id="people" class="Com.Jsxs.pojo.People" autowire="byName"> <property name="name" value="川川"/> </bean> </beans>
4.byType (根据class)
byType :会自动在容器上下文进行查找,和自己对象属性类型相同的bean
(1).正列
配置
<?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 --> <bean id="dog" class="Com.Jsxs.pojo.Dog"/> <bean id="cat22" class="Com.Jsxs.pojo.Cat"/> <!-- byName :会自动在容器上下文进行查找,和自己对象set方法后面的值对应的bean 的id 相等的话,那么就可以使用 byType :会自动在容器上下文进行查找,和自己对象属性相通的bean --> <bean id="people" class="Com.Jsxs.pojo.People" autowire="byType"> <property name="name" value="川川"/> <!-- <property name="cat" ref="cat"/>--> <!-- <property name="dog" ref="dog"/>--> </bean> </beans>
测试
import Com.Jsxs.pojo.People; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); People people = context.getBean("people", People.class); System.out.println(people); people.getDog().shout(); people.getCat().shout(); } }
(2).弊端
必须保证属性全局唯一
5.小结
- byName的时候,需要保证bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致!!
- byType的时候,需要保证bean的class唯一,并且这个bean需要和自动注入的属性的类型一致!!
(八)、注解实现自动装配
1.实现注解须知
- 导入约束
xmlns:context="http://www.springframework.org/schema/context"
- 配置注解支持
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <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" 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"> <context:annotation-config/> </beans>
2.如何使用注解实现自动装配
(1).可以直接在属性上添加 注解
@Autowired
实体类
package Com.Jsxs.pojo; public class Cat { public void shout(){ System.out.println("喵喵喵喵喵..."); } }
package Com.Jsxs.pojo; public class Dog { public void shout(){ System.out.println("汪汪汪..."); } }
package Com.Jsxs.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; @Data @AllArgsConstructor @NoArgsConstructor public class People { @Autowired private Cat cat; @Autowired private Dog dog; private String name; }
配置
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <bean id="dog" class="Com.Jsxs.pojo.Dog"/> <bean id="cat" class="Com.Jsxs.pojo.Cat"/> <bean id="people" class="Com.Jsxs.pojo.People"/> </beans>
测试
import Com.Jsxs.pojo.People; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); People people = context.getBean("people", People.class); System.out.println(people); people.getDog().shout(); people.getCat().shout(); } }
2.@Autowired讲解(先类型后名字)
- [ 直接在属性上使用即可,也可以在set方式上使用
- [ 使用 @Autowired,我们可以不在编写Set方法了,前提是你这个自动装配的属性在IOC(Spring)容器中存在,且符合名字byType;
- 这个注解借助的是映射,并不是set方法依赖注入