6.1实例化方式
3种bean实例化方式:默认构造、静态工厂、实例工厂
6.1.1默认构造
配置的时候形如: <bean id="" class=""> 必须提供默认构造方法 我们以前用的全部都是默认构造
6.1.2静态工厂
- 常用与spring整合其他框架(太小的就叫做工具)
- 静态工厂:用于生成实例对象,所有的方法必须是static
配置的时候形如: <bean id="" class="工厂全限定类名(也就是包名加类名)" factory-method="静态方法">
6.1.2.1工厂
public class MyBeanFactory {
/**
* 创建实例
* @return
*/
public static UserService createService(){
return new UserServiceImpl();
}
}
接口和实现类还是上面的userservice和UserServiceImpl
测试类:
package inject.static_factory;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestStaticFactory {
@Test
public void demo01() {
//老旧的自定义工厂
UserService userservice = MyBeanFactory.createService();
userservice.addUser();
}
@Test
public void demo02() {
//交给spring后的工厂
String xmlpath="inject/static_factory/beans.xml";
ApplicationContext applicationcontext = new ClassPathXmlApplicationContext(xmlpath);
//UserService userservice =(UserService)applicationcontext.getBean("userservice");
//怕一直强制转换麻烦就可以采用getbean()的另一种方法,在名字后再加一个参数,字节码底层自动强制转换
UserService userservice =applicationcontext.getBean("userservice",UserService.class);
userservice.addUser();
}
}
6.1.2.2 spring配置(配置beans.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">
<!-- 将静态工厂创建的实例交给spring
class 静态工厂的类的全包
factory-method 确定静态方法名,因为以后可能一个静态类里边有好几个
方法,那么就可以一个id 对应一个新的方法,但是工厂类不变 只是变i的和factory-method -->
<bean id ="userservice" class="inject.static_factory.MyBeanFactory" factory-method="createService"></bean>
</beans>
6.1.3实例工厂
实例工厂:必须先有工厂实例对象(这是工厂的特性),通过实例对象创建对象。提供所有的方法都是“非静态”的。
6.1.3.1工厂
/**
* 实例工厂,所有方法非静态
*
*/
public class MyBeanFactory {
/**
* 创建实例
* @return
*/
public UserService createService(){
return new UserServiceImpl();
}
}
6.1.3.2spring配置
<!-- 创建工厂实例 -->
<bean id="myBeanFactoryId" class="com.itheima.c_inject.c_factory.MyBeanFactory"></bean>
<!-- 获得userservice
* factory-bean 确定工厂实例
* factory-method 确定普通方法(也就是需要调用的方法)
-->
<!-- 获得userservice(也就是UserServiceImpl向上转型) -->
<bean id="userServiceId" factory-bean="myBeanFactoryId" factory-method="createService"></bean>
6.2Bean种类
普通bean:之前操作的都是普通bean。<bean id="" class="A"> ,spring直接创建A实例,并返回
FactoryBean:是一个特殊的bean,具有工厂生成对象能力,只能生成特定的对象。
bean必须使用 FactoryBean接口,此接口提供方法 getObject() 用于获得特定bean。
<bean id="" class="FB"> 先创建FB实例,使用调用getObject()方法,并返回方法的返回值
FB fb = new FB();
return fb.getObject();
BeanFactory 和 FactoryBean 对比?
BeanFactory:工厂,用于生成任意bean。
FactoryBean:特殊bean,用于生成另一个特定的bean。例如:ProxyFactoryBean ,此工厂bean用于生产代理。<bean id="" class="....ProxyFactoryBean"> 获得代理对象实例。AOP使用
6.3作用域
作用域:用于确定spring创建bean实例个数
取值:
singleton 单例,默认值。
prototype 多例,每执行一次getBean将获得一个实例。例如:struts整合spring,配置action多例。
配置信息
<bean id="" class="" scope="">
<bean id="userServiceId" class="com.itheima.d_scope.UserServiceImpl"
scope="prototype" ></bean>
测试:
实现类和接口:
接口:
package scope;
public interface UserService {
public void addUser();
}
实现类:
package scope;
public class UserServiceImpl implements UserService{
@Override
public void addUser() {
System.out.println("scope add user");
}
}
测试类:
package scope;
public class UserServiceImpl implements UserService{
@Override
public void addUser() {
System.out.println("scope add user");
}
}
6.3.1单例配置:beans.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 = "userService" class="scope.UserServiceImpl" scope="prototype"></bean>
</beans>
运行结果:
原因是:用了默认的单例:
6.3.2prototype 配置beans.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 = "userService" class="scope.UserServiceImpl" scope="prototype"></bean>
</beans>
运行结果:
对比:
差距就是beans.xml文件中是不是scope属性赋值成多例
6.4生命周期(11个步骤,但是不用记住11个就行)
重要的总结如下:
6.4.1初始化和销毁
目标方法执行前后执行后,将进行初始化或销毁。
配置如下就可执行:
<bean id="" class="" init-method="初始化方法名称" destroy-method="销毁的方法名称">
配置成这个样子就行了亲!
测试实例:
6.4.1.1目标类(也就是对应接口的实现类)
public class UserServiceImpl implements UserService {
@Override
public void addUser() {
System.out.println("e_lifecycle add user");
}
public void myInit(){
System.out.println("初始化");
}
public void myDestroy(){
System.out.println("销毁");
}
}
6.4.1.2spring配置
<!--
init-method 用于配置初始化方法,准备数据等
destroy-method 用于配置销毁方法,清理资源等
-->
<bean id="userServiceId" class="com.itheima.e_lifecycle.UserServiceImpl"
init-method="myInit" destroy-method="myDestroy" ></bean>
6.4.1.3测试
小问题1:
因为没有关闭容器
小问题2:
因为为了起到提示作用我偷偷的把beans.xml的多例属性添加上了
把beans.xml改成单例,关闭容器后如下:
package lifecycle;
import java.lang.reflect.InvocationTargetException;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestCycle {
@Test
public void demo01() throws Exception {
//交给spring后的工厂
/* String xmlpath="lifecycle/beans.xml";
ApplicationContext applicationcontext = new ClassPathXmlApplicationContext(xmlpath);
UserService userservice = ( UserService)applicationcontext.getBean("userService");
userservice.addUser();
applicationcontext.getClass().getMethod("close").invoke(applicationcontext );*/
//close()方法接口中是没有的,在实现类中但是就为了一个这采用反射显然不合算,所以直接不用转型了
String xmlpath="lifecycle/beans.xml";
ClassPathXmlApplicationContext applicationcontext = new ClassPathXmlApplicationContext(xmlpath);
UserService userservice = ( UserService)applicationcontext.getBean("userService");
userservice.addUser();
applicationcontext.close();
}
}
@Test
public void demo02() throws Exception{
//spring 工厂
String xmlPath = “com/itheima/e_lifecycle/beans.xml”;
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
UserService userService = (UserService) applicationContext.getBean(“userServiceId”);
userService.addUser();
//要求:1.容器必须close,销毁方法执行; 2.必须是单例的
// applicationContext.getClass().getMethod("close").invoke(applicationContext);
// * 此方法接口中没有定义,实现类提供
applicationContext.close();
}
这个只是把提示加进去了,和上边的一样
6.4.2BeanPostProcessor 后处理Bean
spring 提供一种机制,只要实现此接口BeanPostProcessor,并将实现类提供给spring容器,spring容器将自动执行,在初始化方法前执行before(),在初始化方法后执行after() 。 配置:形如:
<bean class="">
- Factory hook(勾子) that allows for custom modification of new bean instances, e.g. checking for marker interfaces or wrapping them with proxies.
- spring提供工厂勾子,用于修改实例对象,可以生成代理对象,是AOP底层。
模拟过程:
A a =new A();
a = B.before(a) --> 将a的实例对象传递给后处理bean,可以生成代理对象并返回。
此时a已经变成代理类,如果返回空,之后的全是空指针异常
a.init();
a = B.after(a);
a.addUser(); //生成代理对象,目的在目标方法前后执行(例如:开启事务、提交事务)
a.destroy()
测试:
实现类和接口:
package lifecycle;
public interface UserService {
public void addUser();
}
package lifecycle;
public class UserServiceImpl implements UserService{
@Override
public void addUser() {
System.out.println("lifecycle add user");
}
public void myInit() {
System.out.println("初始化方法");
}
public void myDestory() {
System.out.println("方法的摧毁");
}
}
配置后处理bean的处理类:
package lifecycle;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("前方法" + beanName );
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("后方法" + beanName );
return bean;
}
}
把这个处理类,直接配置到文件中,他会被spring自己调用
<?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 = "userService" class="lifecycle.UserServiceImpl"
init-method="myInit" destroy-method="myDestory"></bean>
<bean class="lifecycle.MyBeanPostProcessor"></bean> <!-- 这就是配置后置处理-->
</beans>
测试类:
package lifecycle;
import java.lang.reflect.InvocationTargetException;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestCycle {
@Test
public void demo01() throws Exception {
//交给spring后的工厂
/* String xmlpath="lifecycle/beans.xml";
ApplicationContext applicationcontext = new ClassPathXmlApplicationContext(xmlpath);
UserService userservice = ( UserService)applicationcontext.getBean("userService");
userservice.addUser();
applicationcontext.getClass().getMethod("close").invoke(applicationcontext );*/
//close()方法接口中是没有的,在实现类中但是就为了一个这采用反射显然不合算,所以直接不用转型了
String xmlpath="lifecycle/beans.xml";
ClassPathXmlApplicationContext applicationcontext = new ClassPathXmlApplicationContext(xmlpath);
UserService userservice = ( UserService)applicationcontext.getBean("userService");
userservice.addUser();
applicationcontext.close();
}
}
发现他只是会在初始化这个方法前后出现,现在如果有需要在目标方法前后调用我们的这两个方法如何处理?
A a =new A();
a = B.before(a) --> 将a的实例对象传递给后处理bean,可以生成代理对象并返回。
此时a已经变成代理类,如果返回空,之后的全是空指针异常
a.init();
a = B.after(a);
//生成代理
a.addUser(); //生成代理对象,目的在目标方法前后执行(例如:开启事务、提交事务)
a.destroy()
如下:
6.4.2.1编写实现类
package lifecycle;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("前方法" + beanName );
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("后方法" + beanName );
//生成jdk代理代理 getProxyClass(loader, interfaces,h)
// 第一个参数 loader :当前 类的加载器
// 第二个参数 interfaces 生成代理对象(此时bean是目标对象生成代理对象的时候,要和代理对象的接口数一样)
//第三个参数 处理程序 代理类一执行就会找处理程序
return Proxy.newProxyInstance(MyBeanPostProcessor.class.getClassLoader(),
bean.getClass().getInterfaces(),
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// TODO Auto-generated method stub
System.out.println("开始事务");
Object invoke = method.invoke(bean, args);//执行目标的方法
//对象是bean对象 参数就和你给方法的参数是一样的
System.out.println("提交事务");
return invoke;
}
});
}
}
6.4.2.2配置
<bean class="com.itheima.e_lifecycle.MyBeanPostProcessor"></bean>
-
问题1:后处理bean作用某一个目标类,还是所有目标类?
所有 -
问题2:如何只作用一个?
通过“参数2”beanName进行控制
6.5属性依赖注入
1.依赖注入方式:手动装配 和 自动装配
2.手动装配:一般进行配置信息都采用手动
基于xml装配:构造方法、setter方法
基于注解装配:
3. 自动装配:struts和spring 整合可以自动装配
byType:按类型装配
byName:按名称装配
constructor构造装配,
auto: 不确定装配。
6.5.1构造方法
6.5.1.1目标类
public class User {
private Integer uid;
private String username;
private Integer age;
public User(Integer uid, String username) {
super();
this.uid = uid;
this.username = username;
}
public User(String username, Integer age) {
super();
this.username = username;
this.age = age;
}
6.5.1.2spring配置
<!-- 构造方法注入
* <constructor-arg> 用于配置构造方法一个参数argument
name :参数的名称
value:设置普通数据
ref:引用数据,一般是另一个bean id值
index :参数的索引号,从0开始 。如果只有索引,匹配到了多个构造方法时,默认使用第一个。
type :确定参数类型
例如:使用名称name
<constructor-arg name="username" value="jack"></constructor-arg>
<constructor-arg name="age" value="18"></constructor-arg>
例如2:【类型type 和 索引 index】
<constructor-arg index="0" type="java.lang.String" value="1"></constructor-arg>
<constructor-arg index="1" type="java.lang.Integer" value="2"></constructor-arg>
-->
<bean id="userId" class="com.itheima.f_xml.a_constructor.User" >
<constructor-arg index="0" type="java.lang.String" value="1"></constructor-arg>
<constructor-arg index="1" type="java.lang.Integer" value="2"></constructor-arg>
</bean>
6.5.2setter方法
<!-- setter方法注入
* 普通数据
<property name="" value="值">
等效
<property name="">
<value>值
* 引用数据
<property name="" ref="另一个bean">
等效
<property name="">
<ref bean="另一个bean"/>
-->
1.基本类为了测试多加了一个基本类
地址类:
package setter;
public class Address {
private String addr;//地址
private String tel; //电话
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
@Override
public String toString() {
return "Address [addr=" + addr + ", tel=" + tel + "]";
}
}
person类:
package setter;
public class Person {
private String pname;
private Integer age;
private Address homeAddr;//家庭地址
private Address companyAddr;//公司地址
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Address getHomeAddr() {
return homeAddr;
}
public void setHomeAddr(Address homeAddr) {
this.homeAddr = homeAddr;
}
public Address getCompanyAddr() {
return companyAddr;
}
public void setCompanyAddr(Address companyAddr) {
this.companyAddr = companyAddr;
}
@Override
public String toString() {
return "Person [pname=" + pname + ", age=" + age + ", homeAddr=" + homeAddr + ", companyAddr=" + companyAddr + "]";
}
}
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">
<!-- setter方法注入
* 普通数据
<property name="" value="值">
等效
<property name="">
<value>值
* 引用数据
<property name="" ref="另一个bean">
等效
<property name="">
<ref bean="另一个bean"/>
-->
<bean id = "person" class="setter.Person">
<!--普通的属性直接注入 -->
<property name="pname" value="小红"></property>
<property name="age" value="14"></property>
<!-- 引用的,也就是差不多本身就是一个对象的 直接引入另一个bean的id
而那个对象,直接自己初始化 -->
<property name="homeAddr" ref="homeAddr"></property>
<property name="companyAddr" ref="componeyAddr"> </property>
</bean>
<bean id = "homeAddr" class = "setter.Address">
<property name="addr" value="富贵小区"></property>
<property name="tel" value="222"></property>
</bean>
<bean id = "componeyAddr" class = "setter.Address">
<property name="addr" value="A座"></property>
<property name="tel" value="911"></property>
</bean>
</beans>
3.测试:
package setter;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSetter {
@Test
public void demo01() {
String xmlpath="setter/beans.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlpath);
Person person = (Person)applicationContext.getBean("person");
System.out.println(person);
}
}
4.结果:
6.5.3P命令空间[了解]
- 对“setter方法注入”进行简化,替换
<property name="属性名">,
而是在
<bean p:属性名="普通值" p:属性名-ref="引用值">
2.p命名空间使用前提,必须添加命名空间
其余的和setter的测试案例一样:
我们只是改一下配置文件用p标签简化
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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 = "person" class="p.Person"
p:pname="白占成" p:age="22"
p:homeAddr-ref="homeAddr"
p:companyAddr-ref="companyAddr"
/>
<bean id = "homeAddr" class= "p.Address"
p:addr="中北大学" p:tel="110"
/>
<bean id = "companyAddr" class= "p.Address"
p:addr="田园机房" p:tel="222"
/>
</beans>
6.5.4SpEL[了解]
对进行统一编程,所有的内容都使用value(以前有value 还有ref)
只有name 和 value 这两个属性
#{123}、#{‘jack’} : 数字、字符串 (字符串必须单引号)
#{beanId} :另一个bean引用,如果没有引号,这时候不用引号
#{beanId.propName} :操作数据
#{beanId.toString()} :执行方法
#{T(类).字段|方法} :静态方法或字段
<!--
<property name="cname" value="#{'jack'}"></property>
<property name="cname" value="#{customerId.cname.toUpperCase()}"></property>
通过另一个bean,获得属性,调用的方法
<property name="cname" value="#{customerId.cname?.toUpperCase()}"></property>
?. 如果对象不为null,将调用方法
-->
<bean id="customerId" class="com.itheima.f_xml.d_spel.Customer" >
<property name="cname" value="#{customerId.cname?.toUpperCase()}"></property>
<property name="pi" value="#{T(java.lang.Math).PI}"></property>
</bean>
基础类:
package spel;
public class Customer {
private String username;
private Double Pi;//注入方式模拟静态 Math.PI;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Double getPi() {
return Pi;
}
public void setPi(Double pi) {
Pi = pi;
}
@Override
public String toString() {
return "Customer [username=" + username + ", Pi=" + Pi + "]";
}
}
测试类:
package spel;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpel {
@Test
public void demo01() {
String xmlpath="spel/beans.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlpath);
Customer customer = (Customer)applicationContext.getBean("customer");
System.out.println(customer);
}
}
配置:
<?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 = "customer" class="spel.Customer">
<property name="username" value="#{'jack'}"></property>
</bean>
</beans>
如果出现:
配置文件中没有给username注入值,在基础类中也没有赋值,那么会出现空:
如果继续操作这个空,比如变大写则会报错
但是如果加上?.(问号+点)就不会报错
两种解释:只是单纯的点 和 问号 点
注意:静态方法,变量的调用
格式: #{ T(类的权限名). 方法(常量名)}
例如:
<bean id = "customer" class="spel.Customer">
<!-- <property name="username" value="#{customer.username?.toUpperCase()}"></property> -->
<property name="username" value="白占成"></property>
<property name="Pi" value="#{T(java.lang.Math).PI}"></property>
</bean>
</beans>
阅读:
6.5.5集合注入
<bean id="collDataId" class="com.itheima.f_xml.e_coll.CollData" >
<property name="arrayData">
<array>
<value>DS</value>
<value>DZD</value>
<value>屌丝</value>
<value>屌中屌</value>
</array>
</property>
<property name="listData">
<list>
<value>于嵩楠</value>
<value>曾卫</value>
<value>杨煜</value>
<value>曾小贤</value>
</list>
</property>
<property name="setData">
<set>
<value>停封</value>
<value>薄纸</value>
<value>关系</value>
</set>
</property>
<property name="mapData">
<map>
<entry key="jack" value="杰克"></entry>
<entry>
<key><value>rose</value></key>
<value>肉丝</value>
</entry>
</map>
</property>
<property name="propsData">
<props>
<prop key="高富帅">嫐</prop>
<prop key="白富美">嬲</prop>
<prop key="男屌丝">挊</prop>
</props>
</property>
</bean>
1.基础类:
package coll;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class CollData {
private String[] arrayData;
private List<String>listData;
private Set<String>setData;
private Map<String,String>mapData;
private Properties propsData;
public String[] getArrayData() {
return arrayData;
}
public void setArrayData(String[] arrayData) {
this.arrayData = arrayData;
}
public List<String> getListData() {
return listData;
}
public void setListData(List<String> listData) {
this.listData = listData;
}
public Set<String> getSetData() {
return setData;
}
public void setSetData(Set<String> setData) {
this.setData = setData;
}
public Map<String, String> getMapData() {
return mapData;
}
public void setMapData(Map<String, String> mapData) {
this.mapData = mapData;
}
public Properties getPropsData() {
return propsData;
}
public void setPropsData(Properties propsData) {
this.propsData = propsData;
}
public CollData() {
super();
// TODO Auto-generated constructor stub
}
public CollData(String[] arrayData, List<String> listData, Set<String> setData, Map<String, String> mapData,
Properties propsData) {
super();
this.arrayData = arrayData;
this.listData = listData;
this.setData = setData;
this.mapData = mapData;
this.propsData = propsData;
}
@Override
public String toString() {
return "CollData [\narrayData=" + Arrays.toString(arrayData) + ", \nlistData=" + listData + ", \nsetData=" + setData
+ ", \nmapData=" + mapData + ", \npropsData=" + propsData + "]";
}
}
2.测试类:
package coll;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpel {
@Test
public void demo01() {
String xmlpath="coll/beans.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlpath);
CollData colldata = (CollData)applicationContext.getBean("colldata");
System.out.println(colldata);
}
}
3.配置文件:
<?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 name="colldata" class="coll.CollData"></bean>
</beans>
4.输出结果 我们知道输出对象就是输出类的toString 方法
注意集合的注入就是给属性填数据,就是给添加子标签
集合的注入都是给<property>添加子标签,到底添加哪个子标签就要看,属性的类型
数组:<array>
List:<list>
Set:<set>
Map:<map>
Properties:<props>
一般的话:
普通数据:<value>
引用数据:<ref>
测试一小波:
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">
<!--
集合的注入都是给<property>添加子标签,到底添加哪个子标签就要看,属性的类型
数 组:<array>
List:<list>
Set:<set>
Map:<map>
Properties:<props>
-->
<bean name="colldata" class="coll.CollData">
<property name="arrayData">
<array>
<value>白占成1号</value>
<value>白占成2号</value>
<value>白占成3号</value>
<value>白占成4号</value>
</array>
</property>
<property name="listData">
<list>
<value>鲁班1号</value>
<value>鲁班2号</value>
<value>鲁班3号</value>
<value>鲁班4号</value>
</list>
</property>
</bean>
</beans>
输出:
全测试:
<?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">
<!--
集合的注入都是给<property>添加子标签,到底添加哪个子标签就要看,属性的类型
数 组:<array>
List:<list>
Set:<set>
Map:<map>
Properties:<props> 他的底层也是map
-->
<bean name="colldata" class="coll.CollData">
<property name="arrayData">
<array>
<value>白占成1号</value>
<value>白占成2号</value>
<value>白占成3号</value>
<value>白占成4号</value>
</array>
</property>
<property name="listData">
<list>
<value>鲁班1号</value>
<value>鲁班2号</value>
<value>鲁班3号</value>
<value>鲁班4号</value>
</list>
</property>
<property name="setData">
<set>
<value>白占成</value>
<value>真</value>
<value>是</value>
<value>帅</value>
</set>
</property>
<property name="mapData">
<map>
<entry key="jack" value="杰克"></entry>
<!-- 最好是用上面这种下面这种有点麻烦-->
<entry>
<key><value>rose</value></key>
<value>肉丝</value>
</entry>
</map>
</property>
<property name="propsData">
<props>
<prop key="白占成1号">帅</prop>
<prop key="白占成2号">更帅</prop>
<prop key="白占成3号">更更帅</prop>
</props>
</property>
</bean>
</beans>