public class User {
public User() {
System.out.println("创建对象");
}
public void say(){
System.out.println("我是用户");
}
}
<bean id="user" class="com.jt.pojo.User" scope="prototype">bean>
@Test
public void testUser(){
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
context.getBean("user");
context.getBean("user");
context.getBean("user");
}
如果是单例模式,那构造方法只会执行一次
如果是多例模式。那构造方法会执行多次
<bean id="user" class="com.jt.pojo.User" scope="prototype" lazy-init="true">bean>
1.实例化对象
2.初始化操作 (一般对对象的属性赋值)
3.用户使用对象(调用其中的方法)
4.对象销毁 (一般都是释放资源)
public class User {
private String conn;//数据库链接
//1.实例化对象
public User() {
System.out.println("创建对象");
}
//2.初始化方法
public void init() {
this.conn = "赋值数据源链接";
System.out.println(this.conn);
}
//3.用户调用的方法
public void say() {
System.out.println("我是用户");
}
//4.销毁方法
public void destory() {
this.conn = null;
System.out.println("释放资源" + this.conn + "~~~~");
}
}
<bean id="user" class="com.jt.pojo.User" init-method="init" destroy-method="destroy"/>
这时ApplicationContext里面没有close方法,我们要使用其子类ClassPathXmlApplicationContext
//测试生命周期运行
@Test
public void testlife(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
//获取对象
User user = context.getBean(User.class);
//3.用户调用方法
user.say();
//4.只要容器关闭,则对象销毁
context.close();
}
核心: 对象的创建
当某个角色(可能是一个Java实例,调用者)需要另一个角色(另一个Java实例,被调用者)的协助时,在 传统的程序设计过程中,通常由调用者来创建被调用者的实例。但在Spring里,创建被调用者的工作不再由调用者来完成,因此称为控制反转;创建被调用者 实例的工作通常由Spring容器来完成,然后注入调用者,因此也称为依赖注入。
对象中的属性 ,应该由spring容器动态赋值.
public class User {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User() {
}
public User(Integer id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
<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.jt.pojo.User">
<property name="id" value="101">property>
<property name="name" value="李元芳">property>
bean>
beans>
<bean id="user" class="com.jt.pojo.User">
<constructor-arg name="id" value="102"/>
<constructor-arg name="name" value="兰陵王"/>
bean>
<bean id="user" class="com.jt.pojo.User">
<property name="id" value="101"/>
<property name="name" value="李元芳"/>
<property name="list">
<list>
<value>张三value>
<value>李四value>
<value>王五value>
list>
property>
<property name="set">
<set>
<value>1value>
<value>2value>
<value>3value>
set>
property>
<property name="map">
<map>
<entry key="id" value="1000"/>
<entry key="name" value="tomcat猫"/>
map>
property>
<property name="pro">
<props>
<prop key="proId">110prop>
<prop key="proName">米老鼠prop>
props>
property>
bean>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"
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
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
beans>
public class User { //通过容器为对象的属性赋值
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
import com.jt.pojo.User;
import com.jt.service.UserService;
public class UserController {
//spring容器负责注入Service对象
private UserService userService;
private User user;//代替用户传入的数据
public void setUser(User user) {
this.user = user;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public void addUSer(){
userService.addUser(user);
}
}
<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.jt.pojo.User">
<property name="id" value="100"/>
<property name="name" value="springMVC设计模式"/>
bean>
<bean id="userDao" class="com.jt.dao.UserDaoImpl"/>
<bean id="userService" class="com.jt.service.UserServiceImpl">
<property name="userDao" ref="userDao"/>
bean>
<bean id="userController" class="com.jt.controller.UserController">
<property name="userService" ref="userService"/>
<property name="user" ref="user"/>
bean>
beans>
public class TestSpring {
@Test
public void testSpringMVC(){
ApplicationContext context =
new ClassPathXmlApplicationContext("application.xml");
UserController userController = (UserController) context.getBean("userController");
userController.addUser();
System.out.println("恭喜学会MVC结构");
}
}
由于由于业务需要xml配置文件中 可能会有特殊字符,但是该特殊字符与xml关键字(标签)形成冲突,所以我们要 实现字符串转义
<
小于<>
大于>&
和号&'
单引号’"
引号"