大家好,又见面了,我是你们的朋友全栈君。
Spring装配工厂是为了兼容以前没有Spring的时候,已经写好的工厂代码。
装配
set装配:property
构造装配:constructor-arg
集合装配:Aray List Set 但实际上是通用的
Bean的装配可以理解为依赖关系注入Bean的装配方式即Bean依赖注入的方式.Spring容器支持多种形式的Bean的装配方式,如基于XML的装配、基于注解(Annotation)的装配和自动装配(其中最常用的是基于注解的装配)
需要引入的jar包:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
多例和单例
public static void main(String[] args){
ApplicationContext context= new ClassPathXmlApplicationContext("spring/applicationContext.xml");
UserService userService=context.getBean("userService",UserService.class);
UserService userService2=context.getBean("userService",UserService.class);
System.out.println(userService.hashCode());
System.out.println(userService2.hashCode());
}
spring 容器获得的对象 默认是单例的,如有必要可以在配置文件中修改为多例
<bean class="com.xbj.service.UserServiceImpl" id="userService" scope="prototype">
<!--如果设置了setter 方法 就会提示 ref 引用得是被管理的对象 value 是设置普通值-->
<property name="userDAO" ref="userDAO"/>
</bean>
Bean 的初始化时机
默认情况下,Spring bean的初始化时机,是容器启动的时候,而不是 对象被调用的时候
但是如果一个Bean被设置为多例的,Bean使用的时候才会被创造
装配工厂
非静态方法:必须实例化工厂类(factory-bean)后才能调用工厂方法
实例工厂
public class DAOFactory {
public TopicDAO getTopicDAO(){
return new TopicDAOImpl();
}
public UserDAO getUserDAO(){
return new UserDAOImpl();
}
}
<! --装配实例工厂-->
<bean class="com.xbj.factory.DAOFactory" id="daoFactory"/>
<!--装配Bean -->
<bean id="userDAO" factory-bean="daoFactory" factory-method="getUserDAO"/>
<bean id="topicDAO" factory-bean="daoFactory" factory-method="getTopicDAO"/>
静态方法:无须创建工厂类实例的情况下就可以调用工厂类方法
//静态工厂
public class DAOFactory {
public static TopicDAO getTopicDAO(){
return new TopicDAOImpl();
}
public static UserDAO getUserDAO(){
return new UserDAOImpl();
}
}
public class UserServiceImpl implements UserService{
private UserDAO userDAO=DAOFactory.getUserDAO();
private TopicDAO topicDAO=DAOFactory.getTopicDAO();
public void addUser() {
userDAO.addUser();
}
public void delUser() {
userDAO.delUser();
}
//setter 初始化 UserDAO
public void setUserDAO(UserDAO userDAO) {
System.out.println("通过setter");
this.userDAO = userDAO;
}
<!--装配Bean -->
<bean id="userDAO" class="com.xbj.factory.DAOFactory" factory-method="getUserDAO"/>
<bean id="topicDAO" class="com.xbj.factory.DAOFactory" factory-method="getTopicDAO"/>
factory-bean:用于实例化工厂类;
factory-method:用于调用工厂类方法。
Set方法装配
package com.xbj.po;
public class Address {
private String city;
private String area;
private Integer code;
//getter setter
}
package com.xbj.po;
public class User {
private String name;
private Integer age;
private Address address;
//getter setter
}
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="com.xbj.po.Address" id="address">
<property name="city" value="HF"/>
<property name="area" value="SS" />
<property name="code" value="100100" />
</bean>
<bean class="com.xbj.po.User" id="user">
<property name="name" value="tom"/>
<property name="address" ref="address"/>
<property name="age" value="22" />
</bean>
</beans>
public class Test01 {
@Test
public void t1(){
ApplicationContext context=new ClassPathXmlApplicationContext("spring/applicationContext.xml");
User user=context.getBean("user", User.class);
System.out.println(user.getName());
}
@Test
public void t2(){
ApplicationContext context=new ClassPathXmlApplicationContext("spring/applicationContext.xml");
User user=context.getBean("user", User.class);
System.out.println(user.getAddress().getCity());
}
@Test
public void t3(){
}
}
构造装配
默认情况下(没有指定特定的构造装配Bean) Spring容器 通过 空构造 创造对象,如果没有空构造,会报错:找不到默认的构造
public class Address {
private String city;
private String area;
private Integer code;
public Address() {
}
public Address(String area, Integer code) {
this.area = area;
this.code = code;
}
public Address(String city, String area) {
this.city = city;
this.area = area;
}
//getter setter
//applicationContext.xml
<bean class="com.xbj.po.Address" id="address">
<constructor-arg value="HF" name="area" />
<constructor-arg value="22" name="code" />
</bean>
//Junit 注解 的方法必须是public的
private ApplicationContext context;
@Before
public void init(){
context=new ClassPathXmlApplicationContext("spring/applicationContext-2.xml");
}
@Test
public void t1(){
Address address=context.getBean("address", Address.class);
System.out.println(address);
}
P命名空间
简化setter方法的装配
public class Address {
private String city;
private String area;
private Integer code;
//getter setter
<bean class="com.xbj.po.Address" id="address">
<property name="city" value="HF"/>
<property name="area" value="SS" />
<property name="code" value="100100" />
</bean>
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="comboPooledDataSource">
<property name="jdbcUrl" value="" />
<property name="password" value=" " />
<property name="user" value="" />
<property name="driverClass" value=""/>
</bean>
约束文件添加 xmlns:p="http://www.springframework.org/schema/p"
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="com.xbj.po.Address" id="address" p:area="SS" p:city="HF" p:code="100100" />
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="comboPooledDataSource" p:jdbcUrl=""
p:driverClass="" p:user="" p:password="" />
3.6 集合的装配
装配 List Set Array
public class User
{
private List<String> hobbies;
private String[] books;
private Set<Integer> nums;
//getter setter
<bean class="com.xbj.po.User" id="user">
<!--集合装配 List Set Array (list-set-array 是通用的)-->
<property name="books" >
<array>
<value>sanguo</value>
<value>shuihu</value>
</array>
</property>
<property name="hobbies">
<list>
<value>11</value>
<value>22</value>
</list>
</property>
<property name="nums">
<list>
<value>11</value>
<value>22</value>
</list>
</property>
</bean>
装配
public class User{
private List<String> hobbies;
private String[] books;
private Set<Integer> nums;
private HashMap<String,String> likes;
//getter setter
<property name="likes">
<map>
<entry key="lucy" value="ddd"/>
<entry key="lucy2" value="3333"/>
</map>
</property>
private Properties props;
的本质也是MAP 可以按照MAP去装配
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/106645.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...