bean属性
1. id、name都可以表示bean的名字
id:以前属性中不能有特殊字符,现在放特殊字符也没关系
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="car" class="com.neudeu.po.Car"></bean>
</beans>
Test.java
package com.neuedu.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Object bean = ac.getBean("car");
Object bean2 = ac.getBean("car");
System.out.println(bean == bean2);
}
}
Car.java
package com.neudeu.po;
public class Car {
private String brand;
private double price;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Car [brand=" + brand + ", price=" + price + "]";
}
}
输出:
<bean id="car" class="com.neudeu.po.Car"></bean> 只配置一个 car,
Object bean = ac.getBean("car");Object bean2 = ac.getBean("car"); 无论获取几次但都是同一个对象
System.out.println(bean == bean2);故输出为 ture
2.scope属性:bean的生存范围
1.singleton 单例(默认)
2. proptotype (原型)每次创建新的对象
代码:配置文件: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="car" class="com.neudeu.po.Car" scope="prototype"></bean>
</beans>
Test.java
package com.neuedu.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Object bean = ac.getBean("car");
Object bean2 = ac.getBean("car");
System.out.println(bean == bean2);
}
}
car.java
package com.neudeu.po;
public class Car {
private String brand;
private double price;
public Car() {
System.out.println("Car构造方法");
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Car [brand=" + brand + ", price=" + price + "]";
}
}
输出:
修改为 singleton
<?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="car" class="com.neudeu.po.Car" scope="singleton"></bean>
</beans>
输出:
3.autowire 自动装配(可读性太差)
1.byName 按名字装配
2.byType 按类型装配
文件目录:
代码:配置文件: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="car" class="com.neudeu.po.Car" scope="singleton"></bean> -->
<bean id="idCard" class="com.neudeu.po.idCard">
<property name="cardNo" value="640000200000000011"></property>
</bean>
<bean id ="person" class="com.neudeu.po.Person" autowire="byType" >
</bean>
</beans>
Test.java
package com.neuedu.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
}
}
idCard.java
package com.neudeu.po;
public class idCard {
public idCard() {
System.out.println("IdCard...");
}
private String CardNo;
public String getCardNo() {
return CardNo;
}
public void setCardNo(String cardNo) {
CardNo = cardNo;
}
}
Person.java
package com.neudeu.po;
import com.neudeu.po.idCard;
import java.util.List;
public class Person {
private idCard idCard;
private String name;
private int age;
private List<Car> cars;
public List<Car> getCars() {
return cars;
}
public void setCars(List<Car> cars) {
this.cars = cars;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", cars=" + cars + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public idCard getIdCard() {
return idCard;
}
public Person(){
System.out.println("Person...");
}
public void setIdCard(idCard idCard) {
System.out.println("setIdCard...");
this.idCard = idCard;
}
}
输出:
<bean id ="person" class="com.neudeu.po.Person" autowire="byType" >
autowire="byType" :自动注入(按类型),自动的从 Spring 中找有没有 Person 里需要的 idCard 类型的对象,有点化就自动的将其注入到 Person 的属性里面去
代码:配置文件: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="car" class="com.neudeu.po.Car" scope="singleton"></bean> -->
<bean id="idCard" class="com.neudeu.po.idCard">
<property name="cardNo" value="640000200000000011"></property>
</bean>
<bean id="idCard2" class="com.neudeu.po.idCard">
<property name="cardNo" value="640000200000000012"></property>
</bean>
<bean id ="person" class="com.neudeu.po.Person" autowire="byName"></bean>
</beans>
输出:autowire="byName" :自动注入(按名字装配)
4. lazy-init 延迟加载
false : 立即加载(默认)
true : 用到时加载
代码:配置文件: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="car" class="com.neudeu.po.Car" scope="singleton"></bean> -->
<bean id="idCard" class="com.neudeu.po.idCard">
<property name="cardNo" value="640000200000000011"></property>
</bean>
<bean id="idCard2" class="com.neudeu.po.idCard">
<property name="cardNo" value="640000200000000012"></property>
</bean>
<bean id ="person" class="com.neudeu.po.Person" autowire="byName" lazy-init="true"></bean>
</beans>
输出:与前文相比,Person 都没有执行(延迟加载)
修改代码:Test.java
package com.neuedu.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Object bean = ac.getBean("person"); //加入代码
System.out.println(bean); //加入代码
}
}
输出:只有执行 Object bean = ac.getBean("person"); 后才会执行后续代码
5 init-method="init"
在容器对这个bean初始化时执行该方法
代码:配置文件: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="car" class="com.neudeu.po.Car" scope="singleton"></bean> -->
<bean id="idCard" class="com.neudeu.po.idCard">
<property name="cardNo" value="640000200000000011"></property>
</bean>
<bean id="idCard2" class="com.neudeu.po.idCard">
<property name="cardNo" value="640000200000000012"></property>
</bean>
<bean id ="person" class="com.neudeu.po.Person" autowire="byName" init-method="init">
//修改代码
</bean>
</beans>
Person.java
package com.neudeu.po;
import com.neudeu.po.idCard;
import java.util.List;
public class Person {
private idCard idCard;
private String name;
private int age;
private List<Car> cars;
public List<Car> getCars() {
return cars;
}
public void setCars(List<Car> cars) {
this.cars = cars;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", cars=" + cars + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public idCard getIdCard() {
return idCard;
}
public Person(){
System.out.println("Person...");
}
public void setIdCard(idCard idCard) {
System.out.println("setIdCard...");
this.idCard = idCard;
}
public void init() {
System.out.println("init..."); //修改代码
}
}
输出:bean初始化时执行 init 方法,输出 init...
6. destroy-method="destroy":
容器关闭时执行该方法
代码:配置文件: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 ="person" class="com.neudeu.po.Person" autowire="byName" init-method="init" destroy-method="destroy"></bean>
</beans>
Person.java
package com.neudeu.po;
import com.neudeu.po.idCard;
import java.util.List;
public class Person {
private idCard idCard;
private String name;
private int age;
private List<Car> cars;
public List<Car> getCars() {
return cars;
}
public void setCars(List<Car> cars) {
this.cars = cars;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", cars=" + cars + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public idCard getIdCard() {
return idCard;
}
public Person(){
System.out.println("Person...");
}
public void setIdCard(idCard idCard) {
System.out.println("setIdCard...");
this.idCard = idCard;
}
public void init() {
System.out.println("init...");
}
public void destroy(){
System.out.println("destroy..."); //修改代码
}
}
输出:并没有输出 destory... 要在关闭容器时才会执行
修改代码:Test.java
package com.neuedu.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); //修改代码
Object bean = ac.getBean("person");
System.out.println(bean);
ac.close();
}
}
输出:执行关闭容器,输出 destory...