简介
我们今天来认识一下Spring中最重要的一个知识点 bean, bean是spring核心关键点,一般常用于存在XML文件内进行定义,以及依赖注入,话不多说我们直接开始。
bean
接下来我们开始创建bean
创建一个UserBean.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 class="cn.marinda.resturants.User" name="userBean" >
</bean>
</beans>
接下来开始创建一个User类
public class User {
private String name;
private int age;
public User()
{
}
public User(String name,int age)
{
this.name = name;
this.age = age;
}
public void print()
{
System.out.println("姓名:" + name);
System.out.println("年龄:" + age);
}
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;
}
}
作用域
可以通过作用域来设置是否每次返回一个新的bean实例或者只返回一个bean
列举一下Spring的bean作用域
- singleton
- prototype
- request
- session
- global
接下来的学习我们着重介绍前两种
singleton
是默认的作用域,在没有scope设置前,是用来返回单个bean实例
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 class="cn.marinda.resturants.User" name="userBean" scope="singleton">
</bean>
</beans>
main方法
public static void main(String[] args) {
//创建ClassPathXmlApplicationContext
ApplicationContext context = new ClassPathXmlApplicationContext("userBean.xml");
User user = (User)context.getBean("userBean");
user.setAge(14);
user.print();
User user2 = (User)context.getBean("userBean");
user2.print();
}
结果
姓名:null
年龄:14
姓名:null
年龄:14
prototype
设置之后每次会返回一个新的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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="cn.marinda.resturants.User" name="userBean" scope="prototype">
</bean>
</beans>
main方法
public static void main(String[] args) {
//创建ClassPathXmlApplicationContext
ApplicationContext context = new ClassPathXmlApplicationContext("userBean.xml");
User user = (User)context.getBean("userBean");
user.setAge(14);
user.print();
User user2 = (User)context.getBean("userBean");
user2.print();
}
结果
姓名:null
年龄:14
姓名:null
年龄:0
依赖注入
spring依赖注入有设值函数注入,和构造函数注入
- property
- constructor-arg
还是以前面User类以及UserBean.xml为例
代码实例
property
UserBean.xml
<bean class="cn.marinda.resturants.User" name="userBean" scope="prototype">
<property name="name" value="小明"></property>
<property name="age" value="16"></property>
</bean>
结果
姓名:小明
年龄:14
姓名:小明
年龄:16
constructor-arg
接下来看看 constructor-arg 依赖注入方式
UserBean.xml
<bean class="cn.marinda.resturants.User" name="userBean" scope="prototype">
<constructor-arg name="age" value="16"></constructor-arg>
<constructor-arg name="name" value="小红"></constructor-arg>
</bean>
结果
姓名:小红
年龄:16
姓名:小红
年龄:16
结束语
- spring学习笔记就到这里了,如有不足欢迎补充
- 谢谢观看