1.先导入spring所需的4+1个jar包;
2.编写一个实体类
package wzf.user;
public class User {
private String name;
private String mima;
public String toString() {
return "User [name=" + name + ", mima=" + mima + "]";
}
public User(String name, String mima) {
super();
this.name = name;
this.mima = mima;
System.out.println("有参构造开始:name:"+name+"mima:"+mima);
}
public User() {
System.out.println("无参构造开始");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMima() {
return mima;
}
public void setMima(String mima) {
this.mima = mima;
}
}
3.在工程下创建Source Folder 名为config的目录(存放配置文件<.xml>)
4.创建对象的方法 xml文件+测试类
A:使用无参构造创建对象
app1.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">
<!--作用域:scope="prototype" 多例模式 ,不写默认单例 singleton-->
<!--scope="prototype" 使用到对象时才开始创建
singleton,在启动时(容器初始化之前)创建,并且只有一个
-->
<!--延迟加载
lazy-init="default" 默认为false不延迟,启动时创建
为true时延迟,启动时不创建,使用时创建
-->
<!-- 初始化与销毁
init-method="目标初始化方法"
destroy-method="目标初销毁方法"
-->
<!-- 1:无参构造 -->
<bean id="User" class="wzf.user.User" scope="singleton" >
</bean>
</beans>
测试类
@Test
public void app1() {
String xml="app1.xml";
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(xml);
User user = (User) applicationContext.getBean("User");
System.out.println(user);
}
B:使用带参构造创建对象
app2.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">
<!-- 2:带参构造 -->
<bean id="user" class="wzf.user.User">
<!--设置属性 直接value -->
<!--可以添加 index 确定赋值给哪个属性 -->
<!-- 可以用type 确定类型 -->
<!-- 也可以用ref 应用对象 -->
<constructor-arg index="0" value="wonderful" type="String"></constructor-arg>
<constructor-arg index="1" ref="s" ></constructor-arg>
</bean>
<!-- 创建一个对象并给字符串赋值 -->
<bean id="s" class="java.lang.String">
<constructor-arg value="tom"></constructor-arg>
</bean>
</beans>
测试类:
@Test
public void app2() {
String xml="app2.xml";
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(xml);
User user = (User) applicationContext.getBean("user");
System.out.println(user);
}
C:使用工厂创建对象:
编写工厂类:
package wzf.user;
public class UserFactory {
/*
* 非静态方法
*/
public User getInstance() {
System.out.println("使用工厂类中的非静态方法");
return new User("wonderful","wzf");
}
/*
* 静态方法
*/
public User getStaticInstance() {
System.out.println("使用工厂类中的静态方法");
return new User("wonder","ful");
}
}
app3.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="userFactory" class="wzf.user.UserFactory"></bean>
<bean id="user3" factory-bean="userFactory" factory-method="getInstance"></bean> -->
<!-- 静态方法 -->
<bean id="userFactory" class="wzf.user.UserFactory"></bean>
<bean id="user3" factory-bean="userFactory" factory-method="getStaticInstance"></bean>
<bean id="user3_1" factory-bean="userFactory" factory-method="getInstance"></bean>
</beans>
测试类:
@Test
public void app3() {
String xml="app3.xml";
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(xml);
User user = (User) applicationContext.getBean("user3");
User user1 = (User) applicationContext.getBean("user3_1");
System.out.println(user);
System.out.println("---------------------------------");
System.out.println(user1);
}
结束:谢谢大家支持!-