目录
3.scope(包含属性有:singleton(单例,默认状态),prototype(多例),request,session)
5.1 set方式注入(注入值类型(value)和引用类型(ref))
5.3 复杂类型注入(array,list,set,map,properties)
使用javaEE
新建Dynamic Web Project(记得生成一个web.xml文件。在新建项目时别点finish,而是用两次next,勾选生成web.xml)
导入以下几个jar包
导入spring约束文件:https://blog.csdn.net/qq_40323256/article/details/89738089
User.java:
package com.sikiedu.bean;
public class User {
private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
}
}
1.第一个spring运行实例
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd ">
<bean name="user" class="com.sikiedu.bean.User"></bean>
</beans>
测试代码:HelloSpring.java:
package com.sikiedu.test;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.sikiedu.bean.User;
public class HelloSpring {
@Test
public void Test1(){
ClassPathXmlApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
// User u=(User) ac.getBean("user");
User u=ac.getBean(User.class);
System.out.println(u);
}
}
运行结果:
2.懒加载(lazy-init)
lazy-init="true"时表示“延迟加载”,即创建容器时不加载配置的bean对象,在bean对象被获取的时候才创建
bean对象默认会在容器创建的时候被全部加载出来,如果配置的bean较多,这对硬件的要求较高。所以此时可用延迟加载,不让他一次性加载出来,而是需要bean对象的时候才加载bean对象。
applicationContext.xml:
<bean name="user" class="com.sikiedu.bean.User" lazy-init="true">
<property name="id" value="1"></property>
</bean>
3.scope(包含属性有:singleton(单例,默认状态),prototype(多例),request,session)
包含属性有:
singleton(单例,默认状态):常用。给对象的scope属性设为singleton后,无论创建多少个这个对象,这些对象都是相同的
prototype(多例):创建的每个对象都是不同的
request:不常用
session:不常用
4.init-method,destroy-method
User.java:
package com.sikiedu.bean;
public class User {
private Integer id;
private String username;
private String password;
public User() {
System.out.println("空参构造方法");
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
}
public void InitUser(){
System.out.println("初始化对象");
}
public void DestroyUser(){
System.out.println("对象被消除");
}
}
applicationContext.xml:
<bean name="user" class="com.sikiedu.bean.User" lazy-init="true" scope="singleton" init-method="InitUser" destroy-method="DestroyUser">
<property name="id" value="1"></property>
</bean>
HelloSpring.java:
@Test
public void Test2(){
ClassPathXmlApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
User u1=ac.getBean(User.class);
User u2=ac.getBean(User.class);
User u3=ac.getBean(User.class);
System.out.println(u1==u2);
//关闭容器对象ac,就会触发bean的destroymethod
ac.close();
}
运行结果:
5.属性注入
5.1 set方式注入(注入值类型(value)和引用类型(ref))
Pet.java:
package com.sikiedu.bean;
public class Pet {
private String petType;
private String color;
public String getPetType() {
return petType;
}
public void setPetType(String petType) {
this.petType = petType;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "Pet [petType=" + petType + ", color=" + color + "]";
}
}
User.java:
package com.sikiedu.bean;
public class User {
private Integer id;
private String username;
private String password;
private Pet pet;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Pet getPet() {
return pet;
}
public void setPet(Pet pet) {
this.pet = pet;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + ", pet=" + pet + "]";
}
}
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd ">
<bean name="user" class="com.sikiedu.bean.User">
<property name="id" value="1"></property>
<property name="username" value="王五"></property>
<property name="password" value="555"></property>
<property name="pet" ref="pet"></property>
</bean>
<bean name="pet" class="com.sikiedu.bean.Pet">
<property name="petType" value="二哈"></property>
<property name="color" value="灰色"></property>
</bean>
</beans>
HelloSpring.java:
package com.sikiedu.test;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.sikiedu.bean.User;
public class HelloSpring {
@Test
public void Test1(){
ClassPathXmlApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
User u=ac.getBean(User.class);
System.out.println(u);
}
}
运行结果:
5.2 构造函数注入
User.java:
package com.sikiedu.bean;
public class User {
private Integer id;
private String username;
private String password;
private Pet pet;
public User() {
System.out.println("这是无参构造函数。在使用含参构造函数之前,必须先要写无参构造函数");
}
public User(String username, Pet pet) {
System.out.println("这是含参构造函数。方法1:String,Pet");
this.username = username;
this.pet = pet;
}
public User(Integer id, Pet pet) {
System.out.println("这是含参构造函数。方法2:Integer,Pet");
this.id = id;
this.pet = pet;
}
public User(Pet pet,Integer id) {
System.out.println("这是含参构造函数。方法3:Pet,Integer");
this.id = id;
this.pet = pet;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Pet getPet() {
return pet;
}
public void setPet(Pet pet) {
this.pet = pet;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + ", pet=" + pet + "]";
}
}
applicationContext.xml:
<bean name="pet" class="com.sikiedu.bean.Pet">
<property name="petType" value="二哈"></property>
<property name="color" value="灰色"></property>
</bean>
<!-- 构造方法注入 -->
<!-- type:指定参数类型 -->
<!-- index:指定参数索引 -->
<bean name="user1" class="com.sikiedu.bean.User">
<constructor-arg name="id" value="6" type="java.lang.Integer" index="0"></constructor-arg>
<constructor-arg name="pet" ref="pet"></constructor-arg>
</bean>
HelloSpring.java:
@Test
public void Test2(){
ClassPathXmlApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
User u=(User) ac.getBean("user1");
System.out.println(u);
}
运行结果:
5.3 复杂类型注入(array,list,set,map,properties)
MyCollection.java:
package com.sikiedu.bean;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class MyCollection {
private Object[] array;
private List list;
private Set set;
private Map map;
private Properties properties;
public Object[] getArray() {
return array;
}
public void setArray(Object[] array) {
this.array = array;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Set getSet() {
return set;
}
public void setSet(Set set) {
this.set = set;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public String toString() {
return "MyCollection [array=" + Arrays.toString(array) + ", list=" + list + ", set=" + set + ", map=" + map
+ ", properties=" + properties + "]";
}
}
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd ">
<bean name="user" class="com.sikiedu.bean.User">
<property name="id" value="1"></property>
<property name="username" value="王五"></property>
<property name="password" value="555"></property>
<property name="pet" ref="pet"></property>
</bean>
<bean name="pet" class="com.sikiedu.bean.Pet">
<property name="petType" value="二哈"></property>
<property name="color" value="灰色"></property>
</bean>
<!-- 构造方法注入 -->
<!-- type:指定参数类型 -->
<!-- index:指定参数索引 -->
<bean name="user1" class="com.sikiedu.bean.User">
<constructor-arg name="id" value="6" type="java.lang.Integer" index="0"></constructor-arg>
<constructor-arg name="pet" ref="pet"></constructor-arg>
</bean>
<!-- 复杂类型注入 -->
<bean name="myCollection" class="com.sikiedu.bean.MyCollection">
<property name="array">
<array>
<value>111</value>
<value>aaa</value>
<ref bean="user"/>
</array>
</property>
<property name="list">
<list>
<value>222</value>
<value>bbb</value>
<ref bean="user"/>
</list>
</property>
<property name="set">
<set>
<value>333</value>
<value>ccc</value>
<ref bean="user"/>
</set>
</property>
<property name="map">
<map>
<entry key="username" value="root"></entry>
<entry key="password" value="root"></entry>
<entry key-ref="user" value-ref="pet"></entry>
</map>
</property>
<property name="properties">
<props>
<prop key="name">老李</prop>
<prop key="age">25</prop>
</props>
</property>
</bean>
</beans>
HelloSpring.java:
@Test
public void Test3(){
ClassPathXmlApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
MyCollection mc = (MyCollection) ac.getBean("myCollection");
System.out.println(mc);
}
运行结果:
6.注解配置
6.1 将对象注册到容器
<bean name="user2" class="com.sikiedu.bean.User2">
@Component("user2") //不够精细,因为里面太杂了,什么都有
以后常用的是下面这种,因为分的更细
@Controller() //对应web层
@Service() //对应service层
@Repository() //对应dao层
导入aop包
配置约束
如果已经装了springIDE插件,只需要
如果没有按照springIde插件,添加约束方式参考:https://blog.csdn.net/qq_40323256/article/details/89738089
如果需要按照springIDE插件,安装视频教程:http://www.sikiedu.com/course/267/task/13547/show
User2.java:
package com.sikiedu.bean;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.stereotype.Service;
//<bean name="user2" class="com.sikiedu.bean.User2">
//@Component("user2") //不够精细,因为里面太杂了,什么都有
//@Controller() //对应web层
//@Service() //对应service层
//@Repository() //对应dao层
@Service("user2")
public class User2 {
private Integer id;
private String username;
private String password;
public User2() {
System.out.println("这是无参构造函数。在使用含参构造函数之前,必须先要写无参构造函数");
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User2 [id=" + id + ", username=" + username + ", password=" + password + "]";
}
@PostConstruct()
public void initUser2(){
System.out.println("init User2");
}
@PreDestroy()
public void destroyUser2(){
System.out.println("destroy User2");
}
}
applicationContext_annotation.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 注解开发 -->
<!-- 开启注解扫描,base-package扫描该包及子包的所有注解 -->
<context:component-scan base-package="com.sikiedu.bean"></context:component-scan>
</beans>
HelloSpring.java:
@Test
public void Test4(){
ClassPathXmlApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext_annotation.xml");
User2 u2= (User2) ac.getBean("user2");
System.out.println(u2);
}
运行结果:
6.2 注解配置-属性注入(值类型注入,引用类型注入)
值类型注入:@Value(“”)
引用类型注入: @Autowired() //自动装配。不推荐
@Resource(name="") //手动装配。推荐
Pet.java:
package com.sikiedu.bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("pet")
public class Pet {
private String petType;
private String color;
public String getPetType() {
return petType;
}
@Value("金毛")
public void setPetType(String petType) {
this.petType = petType;
}
public String getColor() {
return color;
}
@Value("黄色")
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "Pet [petType=" + petType + ", color=" + color + "]";
}
}
User2.java:
package com.sikiedu.bean;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
//<bean name="user2" class="com.sikiedu.bean.User2">
//@Component("user2") //不够精细,因为里面太杂了,什么都有
//@Controller() //对应web层
//@Service() //对应service层
//@Repository() //对应dao层
@Service("user2")
public class User2 {
private Integer id;
private String username;
private String password;
private Pet pet;
public User2() {
System.out.println("这是无参构造函数。在使用含参构造函数之前,必须先要写无参构造函数");
}
public Integer getId() {
return id;
}
@Value("2")
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
@Value("李疆疆")
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
@Value("2333")
public void setPassword(String password) {
this.password = password;
}
public Pet getPet() {
return pet;
}
// @Autowired() //自动装配。不推荐
@Resource(name="pet") //手动装配。推荐
public void setPet(Pet pet) {
this.pet = pet;
}
@PostConstruct()
public void initUser2(){
System.out.println("init User2");
}
@PreDestroy()
public void destroyUser2(){
System.out.println("destroy User2");
}
@Override
public String toString() {
return "User2 [id=" + id + ", username=" + username + ", password=" + password + ", pet=" + pet + "]";
}
}
HelloSpring.java:
@Test
public void Test4(){
ClassPathXmlApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext_annotation.xml");
User2 u2= (User2) ac.getBean("user2");
System.out.println(u2);
}
运行结果:
7.Spring整合Junit单元测试(@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext_annotation.xml"))
导入test包
Pet.java:
package com.sikiedu.bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("pet")
public class Pet {
private String petType;
private String color;
public String getPetType() {
return petType;
}
@Value("金毛")
public void setPetType(String petType) {
this.petType = petType;
}
public String getColor() {
return color;
}
@Value("黄色")
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "Pet [petType=" + petType + ", color=" + color + "]";
}
}
applicationContext_annotation.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 注解开发 -->
<!-- 开启注解扫描,base-package扫描该包及子包的所有注解 -->
<context:component-scan base-package="com.sikiedu.bean"></context:component-scan>
</beans>
test.java:
package com.sikiedu.test;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.sikiedu.bean.Pet;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext_annotation.xml")
public class test {
@Resource(name="pet")
private Pet pet;
@Test
public void Test(){
System.out.println(pet);
}
}
运行结果:
8.主配置文件的分包配置(<import resource="xxx.xml"/>)
Pet.java:
package com.sikiedu.bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("pet")
public class Pet {
private String petType;
private String color;
public String getPetType() {
return petType;
}
@Value("金毛")
public void setPetType(String petType) {
this.petType = petType;
}
public String getColor() {
return color;
}
@Value("黄色")
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "Pet [petType=" + petType + ", color=" + color + "]";
}
}
applicationContext_test.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<import resource="applicationContext_annotation.xml"/>
</beans>
applicationContext_annotation.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 注解开发 -->
<!-- 开启注解扫描,base-package扫描该包及子包的所有注解 -->
<context:component-scan base-package="com.sikiedu.bean"></context:component-scan>
</beans>
test.java:
package com.sikiedu.test;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.sikiedu.bean.Pet;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext_test.xml")
public class test {
@Resource(name="pet")
private Pet pet;
@Test
public void Test(){
System.out.println(pet);
}
}
运行结果: