Bean实例化的三种方式
具体代码如下,本实例是用Maven写的,三种方式均需要相同的pom.xml
配置文件。
pom.xml
的配置文件如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zh</groupId>
<artifactId>spring-test</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
</dependencies>
</project>
1. 使用类的无参构造创建(推荐)
spring-context.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.zh.spring.test.entity.User"></bean>
</beans>
User.java
实体类
public class User {
private String username;
private String height;
public User() {
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getHeight() {
return height;
}
public void setHeight(String height) {
this.height = height;
}
}
UserTest.java
用户测试类
public class UserTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
User user = (User) context.getBean("user");
System.out.println(user);
}
}
打印结果如下:
com.zh.spring.test.entity.User@d2cc05a
,因为User.java
中没有重写toString()方法
注意:当User.java
的实体类中将无参构造方法改为有参构造方法时(即User实体类中没有无参构造方法),会报默认构造找不到异常No default constructor found;
2. 使用静态工厂方式创建
spring-context.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="bean2" class="com.zh.spring.test.BeanFactory" factory-method="getBean2"></bean>
</beans>
Bean2.java
public class Bean2 {
public void add() {
System.out.println("Bean2--------------");
}
}
BeanFactory.Java
工厂类
public class BeanFactory {
public static Bean2 getBean2() {
return new Bean2();
}
}
BeanTest.java
public class BeanTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
Bean2 bean2 = (Bean2) context.getBean("bean2");
System.out.println(bean2);
}
}
打印结果如下:
com.zh.spring.test.Bean2@7823a2f9
,因为User.java
中没有重写toString()方法
3. 使用实例工厂创建
spring-context.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="beanFactory" class="com.zh.spring.test.BeanFactory"></bean>
<bean id="bean3" factory-bean="beanFactory" factory-method="getBean3"></bean>
</beans>
Bean3.java
public class Bean3 {
public void add() {
System.out.println("Bean3--------------");
}
}
BeanFactory.java
public class BeanFactory {
public Bean3 getBean3() {
return new Bean3();
}
}
BeanTest.java
public class BeanTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
Bean3 bean3 = (Bean3) context.getBean("bean3");
System.out.println(bean3);
}
}
打印结果如下:
com.zh.spring.test.Bean3@7823a2f9
,因为User.java
中没有重写toString()方法
网友评论