最近在看spring实战,目前在学习第二章,spring bean的装载方法,一共有三种
- xml
- javaConfig
- autoConfig
xml是书写xml配置文件配置bean,javaConfig是在java代码里配置bean,autoConfig是用注解,开启component-scan自动扫描装配。
1.xml配置方式对象实例化
1.1.无参构造实例化对象
定义一个字符串,一个void方法
package com.fc.test.xmltest;
public class Car {
public String noobCar = "这是一辆车";
public void carTest() {
System.out.println("车and车");
}
}
applicationContext.xml
通过<bean></bean>定义car 这个bean
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="car" class="com.fc.test.xmltest.Car" />
</beans>
测试类
通过ClassPathXmlApplicationContext读取xml中定义的bean
package com.fc.test.xmltest;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Car car = (Car) context.getBean("car");
car.carTest();
System.out.println(car.noobCar);
}
}
测试结果
车and车
这是一辆车
1.2.使用静态工厂进行创建
beanFactory
public class BeanFactory {
/**
* 静态方法,返回car对象
* @param:
* @return: car
*/
public static Car getCar() {
return new Car();
}
}
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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--无参构造方法-->
<!--<bean id="car" class="com.fc.test.xmltest.Car" />-->
<!--静态工厂方法-->
<bean id="beanFactory" class="com.fc.test.xmltest.BeanFactory" factory-method="getCar"/>
</beans>
测试类
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Car car = (Car) context.getBean("beanFactory");
car.carTest();
System.out.println(car.noobCar);
}
}
测试结果
车and车
这是一辆车
1.3.使用实例工厂进行创建
beanUFactory
public class BeanUFactroy {
/**
* 普通方法,返回car对象
* @param:
* @return:
*/
public Car getCar() {
return new Car();
}
}
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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--无参构造方法-->
<!--<bean id="car" class="com.fc.test.xmltest.Car" />-->
<!--静态工厂方法-->
<!--<bean id="beanFactory" class="com.fc.test.xmltest.BeanFactory" factory-method="getCar"/>-->
<!--实例工厂方法-->
<!--1.先创建工厂对象-->
<bean id="beanUFactory" class="com.fc.test.xmltest.BeanUFactroy"/>
<!--2.再创建car对象-->
<bean id="carUFactory" factory-bean="beanUFactory" factory-method="getCar"/>
</beans>
测试类
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Car car = (Car) context.getBean("carUFactory");
car.carTest();
System.out.println(car.noobCar);
}
测试结果
车and车
这是一辆车
2.属性注入
2.1.使用有参构造方法注入属性
Car类修改如下
public class Car {
private String name ;
/**
* 无参构造函数
*/
public Car() {
}
/**
* 有参构造函数
*/
public Car(String name) {
this.name = name;
}
public void carTest() {
System.out.println("这是一辆"+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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--无参构造方法-->
<!--bean id="car" class="com.fc.test.xmltest.Car" />-->
<!--有参构造方法-->
<bean id="car" class="com.fc.test.xmltest.Car">
<constructor-arg name="name" value="benci">
</constructor-arg>
</bean>
<!--静态工厂方法-->
<!--<bean id="beanFactory" class="com.fc.test.xmltest.BeanFactory" factory-method="getCar"/>-->
<!--实例工厂方法-->
<!--1.先创建工厂对象-->
<!--<bean id="beanUFactory" class="com.fc.test.xmltest.BeanUFactroy"/>-->
<!--2.再创建car对象-->
<!--<bean id="carUFactory" factory-bean="beanUFactory" factory-method="getCar"/>-->
</beans>
测试类
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Car car = (Car) context.getBean("car");
car.carTest();
}
}
测试结果
这是一辆benci
2.2. 使用set方法注入属性
Car类改为
public class Car {
private String name ;
/**
* set 方法
*/
public void setName(String name) {
this.name = name;
}
public void carTest() {
System.out.println("这是一辆"+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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--无参构造方法-->
<!--bean id="car" class="com.fc.test.xmltest.Car" />-->
<!--有参构造方法-->
<!--<bean id="car" class="com.fc.test.xmltest.Car">
<constructor-arg name="name" value="benci">
</constructor-arg>
</bean>-->
<!--set方法注入-->
<bean id="car" class="com.fc.test.xmltest.Car">
<property name="name" value="bmw">
</property>
</bean>
<!--静态工厂方法-->
<!--<bean id="beanFactory" class="com.fc.test.xmltest.BeanFactory" factory-method="getCar"/>-->
<!--实例工厂方法-->
<!--1.先创建工厂对象-->
<!--<bean id="beanUFactory" class="com.fc.test.xmltest.BeanUFactroy"/>-->
<!--2.再创建car对象-->
<!--<bean id="carUFactory" factory-bean="beanUFactory" factory-method="getCar"/>-->
</beans>
测试类
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Car car = (Car) context.getBean("car");
car.carTest();
}
}
测试结果
这是一辆bmw
2.3.注入对象属性类型
新建company类
public class Company {
private String name;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Car类
public class Car {
private String name ;
private Company company;
/**
* set 方法
*/
public void setCompany(Company company) {
this.company = company;
}
/**
* set 方法
*/
public void setName(String name) {
this.name = name;
}
public void carTest() {
System.out.println("这是一辆"+name+","+"公司名称是:"+company.getName()+","+"公司地址是:"+company.getAddress());
}
}
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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--无参构造方法-->
<!--bean id="car" class="com.fc.test.xmltest.Car" />-->
<!--有参构造方法-->
<!--<bean id="car" class="com.fc.test.xmltest.Car">
<constructor-arg name="name" value="benci">
</constructor-arg>
</bean>-->
<bean id="company" class="com.fc.test.xmltest.Company">
<property name="name" value="daimule"/>
<property name="address" value="europan"/>
</bean>
<!--set方法注入-->
<bean id="car" class="com.fc.test.xmltest.Car">
<property name="name" value="bmw" />
<property name="company" ref="company"/>
</bean>
<!--静态工厂方法-->
<!--<bean id="beanFactory" class="com.fc.test.xmltest.BeanFactory" factory-method="getCar"/>-->
<!--实例工厂方法-->
<!--1.先创建工厂对象-->
<!--<bean id="beanUFactory" class="com.fc.test.xmltest.BeanUFactroy"/>-->
<!--2.再创建car对象-->
<!--<bean id="carUFactory" factory-bean="beanUFactory" factory-method="getCar"/>-->
</beans>
测试方法
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Car car = (Car) context.getBean("car");
car.carTest();
}
}
测试结果
这是一辆bmw,公司名称是:daimule,公司地址是:europan
2.4.注入复杂类型
Company改为
3.总结
通过xml创建bean ,在<bean>标签中指定id class 等属性,通过ClassPathXmlApplicationContext读取applicationContext.xml,然后获取bean,获得对象实例。也可以通过静态工厂方法或者实例工厂方法创建bean.
属性的注入,可以在类里定义有参构造函数,通过<constructor-arg>,进行属性注入
或者通过set方法,通过<property>标签进行属性注入。
网友评论