spring配置:
1.导入最小配置jar包
commons-logging-1.2.jar
spring-beans-4.3.3.RELEASE.jar
spring-context-4.3.3.RELEASE.jar
spring-core-4.3.3.RELEASE.jar
spring-expression-4.3.3.RELEASE.jar
2.创建 beans.xml
new : 创建spring插件中的xml
3.创建 bean
4.在 beans.xml中配置bean,相当于让spring管理这个bean了,那么要获取对象,就直接从 springIOC容器中取。
5.测试
ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");
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="user" class="com.xxy.bean.User" scope="prototype"/>
</beans>
测试
ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");
User user = (User)act.getBean("user");
System.out.println(user);
结果
User [name=null]
不需要通过new对象,就能获取到实体类
6.Bean的属性注入方式
a.通过set方法去注入值
public class Product {
private String name;
private Double price;
private Categroy categroy;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Categroy getCategroy() {
return categroy;
}
public void setCategroy(Categroy categroy) {
this.categroy = categroy;
}
}
<bean id="myProduct" class="com.xxy.bean.Product">
<property name="id" value="1"/>
<property name="name" value="平板电脑"/>
<property name="price" value="566.00"/>
<property name="categroy" ref="categroy"/>
</bean>
<bean id="categroy" class="com.xxy.bean.Categroy"/>
b.通过构造方法
public class Student {
private String name;
private Book book;
public Student() {
super();
}
public Student(String name) {
this.name = name;
}
public Student(Book book){
this.book = book;
}
}
<bean id="student" class="com.xxy.bean2.Student">
<constructor-arg name="book" ref="book"/>
</bean>
<bean id="book" class="com.xxy.bean2.Book"/>
c.通过静态方法实例化对象[这个对象来自于方法的返回值]
public class TransitionFactory {
private static TransitionFactory transitionFactory ;
private TransitionFactory(){}
public static TransitionFactory getTransitionFactory(){
if(transitionFactory==null){
transitionFactory = new TransitionFactory();
}
return transitionFactory;
}
public void test(){
System.out.println("使用静态方法创建对象......");
}
}
<bean id="transition" class="com.xxy.bean3.TransitionFactory" factory-method="getTransitionFactory" />
d.通过非静态方法实例化对象
public class DaoFactory {
public ProductDaoFactory getFactory(){
return new ProductDaoFactory();
}
}
bean id="factory" class="com.xxy.bean4.DaoFactory"/>
<bean id="productDaoFactory" factory-bean="factory" factory-method="getFactory"/>
7.scope选项
a. singleton:单例模式,即该bean对应的类只有一个实例;在spring 中是scope(作用范围)参数的默认值 ;
b. prototype:表示每次从容器中取出bean时,都会生成一个新实例;相当于new出来一个对象;
c. request:基于web,表示每次接受一个请求时,都会生成一个新实例;
d. session:表示在每一个session中只有一个该对象.
//初始化spring容器
ApplicationContext act = new ClassPathXmlApplicationContext("bean1.xml");
Product product1 = (Product)act.getBean("product01");
Product product2 = (Product)act.getBean("product01");
System.out.println(product1);
System.out.println(product1 == product2);
结果
Product [id=123, name=电脑, price=566.6, categroy=null]
true
更改scope值为prototype
<bean id="product01" class="com.xxy.bean.Product" scope="prototype" lazy-init="true">
<property name="id" value="123"/>
<property name="name" value="电脑"/>
<property name="price" value="566.6"/>
</bean>
运行测试1结果
Product [id=123, name=电脑, price=566.6, categroy=null]
false
8.对象的获取,通过key或者通过类
配置两个
<bean id="product" class="com.xxy.bean.Product"></bean>
<!-- scope: 配置是否单例:prototype,singleton ;lazy-init :是否延迟加载(不常用)-->
<bean id="product01" class="com.xxy.bean.Product" scope="prototype" lazy-init="true">
<property name="id" value="123"/>
<property name="name" value="电脑"/>
<property name="price" value="566.6"/>
</bean>
//初始化spring容器
ApplicationContext act = new ClassPathXmlApplicationContext("bean1.xml");
//获取对象1:通过key,2:通过class对象
Product product1 = (Product)act.getBean("product01");
//注意使用 class对象获取 bean,bean必须是唯一的
Product product = act.getBean(Product.class);
当bean.xml对一个类配置多次时,通过class获取会报错.但是此方法获取对象是可行的