Spring Bean标签说明及实例化方式
1.Bean标签
1.scope=”prototype”,默认值,Ioc容器只会创建一个该类型的对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| package com.course.vo;
public class User {
private Integer id; private String name; private String address; 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 String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
|
1 2 3 4 5 6 7 8 9 10 11
| <?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="user1" class="com.course.vo.User"></bean> <bean id="user3" class="com.course.vo.User"></bean> </beans>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| package com.test;
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.course.vo.User;
public class TestMain {
public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); User user1 = (User) applicationContext.getBean("user1"); User user2 = (User) applicationContext.getBean("user1"); System.out.println(user1); System.out.println(user2); System.out.println(user1 == user2);
User user3 = (User) applicationContext.getBean("user3"); System.out.println(user3); System.out.println(user1 == user3); } }
|
2.scope=”prototype”,每次调用IoC容器都会新创建一个该类型的对象,
1 2 3 4 5 6 7 8 9 10
| <?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="user1" class="com.course.vo.User" scope="prototype"></bean> </beans>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package com.test;
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.course.vo.User;
public class TestMain {
public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); User user1 = (User) applicationContext.getBean("user1"); User user2 = (User) applicationContext.getBean("user1"); System.out.println(user1); System.out.println(user2); System.out.println(user1 == user2); } }
|
3.scope=”singleton”,只要Spring启动加载了applicationContext.xml文件,就会创建对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| package com.course.vo;
public class User {
private Integer id; private String name; private String address; public User() { super(); System.out.println("创建User对象"); } 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 String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
|
1 2 3 4 5 6 7 8 9
| <?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="user1" class="com.course.vo.User"></bean> </beans>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.test;
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.course.vo.User;
public class TestMain {
public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); } }
|
4.scope=”prototype”,只有在从获取IoC容器中获取对象的时候,才会创建对象,获取几次对象,就创建几次对象,不去获取对象,就不创建对象。
1 2 3 4 5 6 7 8 9
| <?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="user1" class="com.course.vo.User" scope="prototype"></bean> </beans>
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.test;
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.course.vo.User;
public class TestMain {
public static void main(String[] args) { } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package com.test;
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.course.vo.User;
public class TestMain {
public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); User user1 = (User) applicationContext.getBean("user1"); User user2 = (User) applicationContext.getBean("user1"); } }
|
5.<bean>
标签的属性配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <?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="user1" class="com.course.vo.User" scope="singleton" lazy-init="true"></bean> </beans>
|
6.scope
属性与lazy-init
属性
|scope|lazy-init|结果|
|-|-|-|
|singleton|true|调用对象时才创建对象,不调用对象就不创建,多次调用同一个bean,只会创建一个|
|singleton|false|在IoC容器启动时就会创建对象,只会创建一个|
|prototype|true/false|均为懒加载,调用时才创建对象,不调用不创建对象,每次调用都创建一个新的对象|
7.创建一个Date对象
1 2 3 4 5 6 7 8 9
| <?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="date1" class="java.util.Date"></bean> </beans>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.test;
import java.util.Date;
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestMain {
public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); Date date1 = (Date) applicationContext.getBean("date1"); System.out.println(date1); } }
|
2.Spring实例化Bean的3种方式
1.使用类的无参的构造方式
1 2 3 4 5 6 7 8 9
| <?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="user2" class="com.course.vo.User"></bean> </beans>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package com.test;
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.course.vo.User;
public class TestMain {
public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); User user2 = (User) applicationContext.getBean("user2"); System.out.println(user2); } }
|
2.使用静态工厂
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package com.course.factory;
import com.course.vo.User;
public class UserFactory {
public static User getUser1() { User user = new User(); user.setId(1); user.setName("name1"); user.setAddress("address1"); return user; } public static User getUser2() { User user = new User(); user.setId(2); user.setName("name2"); user.setAddress("address2"); return user; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?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="user1" class="com.course.factory.UserFactory" factory-method="getUser1"></bean> <bean id="user2" class="com.course.factory.UserFactory" factory-method="getUser2"></bean> </beans>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| package com.test;
import java.util.Date;
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.course.vo.User;
public class TestMain {
public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); User user1 = (User) applicationContext.getBean("user1"); User user2 = (User) applicationContext.getBean("user2"); System.out.println(user1); System.out.println(user2); } }
|
3.使用实例工厂
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| package com.course.factory;
import com.course.vo.User;
public class UserFactory {
public User getUser3() { User user = new User(); user.setId(3); user.setName("name3"); user.setAddress("address3"); return user; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <?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="com.course.factory.UserFactory"></bean>
<bean id="user3" factory-bean="userFactory" factory-method="getUser3"></bean> </beans>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package com.test;
import java.util.Date;
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.course.vo.User;
public class TestMain {
public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); User user3 = (User) applicationContext.getBean("user3"); System.out.println(user3); } }
|
3.Bean属性的注入
1.使用set()方法注入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| package com.course.vo;
import java.util.Date;
public class User {
private Integer id; private String name; private String address; private Date hireDate; public User() { super(); } 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 String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Date getHireDate() { return hireDate; } public void setHireDate(Date hireDate) { this.hireDate = hireDate; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", address=" + address + ", hireDate=" + hireDate + "]"; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <?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="date1" class="java.util.Date"></bean> <bean id="user1" class="com.course.vo.User"> <property name="id" value="1"></property> <property name="name" value="name1"></property> <property name="address" value="address1"></property> <property name="hireDate" ref="date1"></property> </bean> </beans>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package com.test;
import java.util.Date;
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.course.vo.User;
public class TestMain {
public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); User user1 = (User) applicationContext.getBean("user1"); System.out.println(user1); } }
|
2.使用有参的构造方法注入
(1)使用<constructor-arg index="2">
,index指定构造方法的参数的索引位置,会根据构造方法的形参去选择相应的构造方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| package com.course.vo;
import java.util.Date;
public class User {
private Integer id; private String name; private String address; private Date hireDate; public User() { super(); } public User(Integer id, String address, String name) { super(); this.id = id; this.name = name; this.address = address; }
public User(Integer id, String name) { super(); this.id = id; this.name = name; } public User(Integer id, String name, String address, Date hireDate) { super(); this.id = id; this.name = name; this.address = address; this.hireDate = hireDate; }
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 String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Date getHireDate() { return hireDate; } public void setHireDate(Date hireDate) { this.hireDate = hireDate; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", address=" + address + ", hireDate=" + hireDate + "]"; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| <?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="user1" class="com.course.vo.User"> <constructor-arg index="0" value="1"></constructor-arg> <constructor-arg index="1" value="param1"></constructor-arg> </bean> </beans>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.test;
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.course.vo.User;
public class TestMain {
public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); User user1 = (User) applicationContext.getBean("user1"); System.out.println(user1); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| <?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="user1" class="com.course.vo.User"> <constructor-arg index="0" value="1"></constructor-arg> <constructor-arg index="1" value="param1"></constructor-arg> <constructor-arg index="2" value="param2"></constructor-arg> </bean> </beans>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.test;
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.course.vo.User;
public class TestMain {
public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); User user1 = (User) applicationContext.getBean("user1"); System.out.println(user1); } }
|
(2)使用<constructor-arg name=""
,name指定构造方法的参数名
1 2 3 4 5 6 7 8 9 10 11 12 13
| <?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="user1" class="com.course.vo.User"> <constructor-arg name="id" value="1"></constructor-arg> <constructor-arg name="name" value="param1"></constructor-arg> <constructor-arg name="address" value="param2"></constructor-arg> </bean> </beans>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.test;
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.course.vo.User;
public class TestMain {
public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); User user1 = (User) applicationContext.getBean("user1"); System.out.println(user1); } }
|
(3) 其他属性ref
和type
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <?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="date1" class="java.util.Date"></bean>
<bean id="user1" class="com.course.vo.User"> <constructor-arg name="id" value="1" type="java.lang.Integer"></constructor-arg> <constructor-arg name="name" value="param1" type="java.lang.String"></constructor-arg> <constructor-arg name="address" value="param2" type="java.lang.String"></constructor-arg> <constructor-arg name="hireDate" ref="date1" type="java.util.Date"></constructor-arg> </bean> </beans>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package com.test;
import java.util.Date;
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.course.vo.User;
public class TestMain {
public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); User user1 = (User) applicationContext.getBean("user1"); System.out.println(user1); } }
|