JavaEE(SSM)企业应用实战 chapter07

一、依赖注入

1、构造器注入

(1)Student.java
package com.qf.chapter07.bean;
​
public class Student {
    private int sid;
    private String name;
    private String age;
    private String course;
​
    public Student(int sid, String name, String age, String course) {
        super();
        this.sid = sid;
        this.name = name;
        this.age = age;
        this.course = course;
    }
​
    @Override
    public String toString() {
        return "Student [sid=" + sid + ", name=" + name + ", age=" + age + ", course=" + course + "]";
    }
}
(2)applicationContext.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"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       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.xsd">
​
    <!-- 通过构造器注入成员变量的值 -->
    <bean id="student" class="com.qf.chapter07.bean.Student">
        <constructor-arg name="sid" value="1"></constructor-arg>
        <constructor-arg name="name" value="ZhangSan"></constructor-arg>
        <constructor-arg name="age" value="20"></constructor-arg>
        <constructor-arg name="course" value="Java"></constructor-arg>
    </bean>
    
</beans>
(3)TestSpring01.java
package com.qf.chapter07.test;
​
import com.qf.chapter07.bean.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
​
public class TestSpring01 {
    public static void main(String[] args) throws Exception {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = applicationContext.getBean("student", Student.class);
        // 输出Student类对象信息
        System.out.println(student);
    }
}

2、属性注入

(1)Teacher.java
package com.qf.chapter07.bean;
​
public class Teacher {
    private int tid;
    private String name;
​
    public void setTid(int tid) {
        this.tid = tid;
    }
​
    public void setName(String name) {
        this.name = name;
    }
​
    @Override
    public String toString() {
        return "Teacher [tid=" + tid + ", name=" + name + "]";
    }
}
(2)applicationContext.xml(新增)
    <bean id="teacher" class="com.qf.chapter07.bean.Teacher">
        <property name="tid" value="1"></property>
        <property name="name" value="LiSi"></property>
    </bean>
(3)TestSpring02.java
package com.qf.chapter07.test;
​
import com.qf.chapter07.bean.Teacher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
​
public class TestSpring02 {
    public static void main(String[] args) throws Exception {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Teacher teacher = applicationContext.getBean("teacher", Teacher.class);
        // 输出Teacher类对象信息
        System.out.println(teacher);
    }
}

二、Bean的配置

1、注入集合

(1)Mix.java
package com.qf.chapter07.bean;
​
import java.util.*;
​
public class Mix {
    private List<String> myList;
    private Map<String, String> myMap;
    private String[] myArray;
​
    public void setMyList(List<String> myList) {
        this.myList = myList;
    }
​
    public void setMyMap(Map<String, String> myMap) {
        this.myMap = myMap;
    }
​
    public void setMyArray(String[] myArray) {
        this.myArray = myArray;
    }
​
    @Override
    public String toString() {
        return "Mix [myList=" + myList + ", myMap=" + myMap + ", myArray=" + Arrays.toString(myArray) + "]";
    }
}
(2)applicationContext.xml(新增)
<bean id="mix" class="com.qf.chapter07.bean.Mix">
        <!-- 注入List -->
        <property name="myList">
            <list>
                <value>list01</value>
                <value>list02</value>
            </list>
        </property>
        <!-- 注入Map -->
        <property name="myMap">
            <map>
                <entry key="key01" value="map01"></entry>
                <entry key="key02" value="map02"></entry>
            </map>
        </property>
        <!-- 注入array -->
        <property name="myArray">
            <array>
                <value>array01</value>
                <value>array02</value>
            </array>
        </property>
    </bean>
(3)TestSpring03.java
package com.qf.chapter07.test;

import com.qf.chapter07.bean.Mix;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring03 {
	public static void main(String[] args) throws Exception {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		Mix mix = applicationContext.getBean("mix", Mix.class);
		// 输出Mix类对象信息
		System.out.println(mix);
	}
}

2、注入其他Bean

(1)School.java
package com.qf.chapter07.bean;

public class School {
	private int sid;
	private Student stu;

	public int getSid() {
		return sid;
	}

	public void setSid(int sid) {
		this.sid = sid;
	}

	public void setStu(Student stu) {
		this.stu = stu;
	}

	public Student getStu() {
		return stu;
	}
}
(2)applicationContext.xml(新增)
	<!--<bean id="school" class="com.qf.chapter07.bean.School">
		<property name="stu">
			<ref bean="student"/>
		</property>
	</bean>-->
	<bean id="school" class="com.qf.chapter07.bean.School">
		<property name="stu" ref="student"></property>
	</bean>
(3)TestSpring04.java
package com.qf.chapter07.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.qf.chapter07.bean.School;
import com.qf.chapter07.bean.Student;

public class TestSpring04 {
	public static void main(String[] args) throws Exception {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		School school = applicationContext.getBean("school", School.class);
		// 通过School对象获取Student对象,并输出Student对象的信息
		Student stu = school.getStu();
		System.out.println(stu);
	}
}

3、使用P:命名空间注入

(1)引入P:命名空间,修改配置文件 applicationContext.xml 中 beans 元素代码
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xmlns:p="http://www.springframework.org/schema/p"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
(2)applicationContext.xml(新增)
	<!--<bean id="school" class="com.qf.chapter07.bean.School">
		<property name="stu">
			<ref bean="student"/>
		</property>
	</bean>
	<bean id="school" class="com.qf.chapter07.bean.School">
		<property name="stu" ref="student"></property>
	</bean>-->
	<bean id="school" class="com.qf.chapter07.bean.School" p:sid="1"
		  p:stu-ref="student"></bean>
(3)TestSpring05.java
package com.qf.chapter07.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.qf.chapter07.bean.School;
import com.qf.chapter07.bean.Student;

public class TestSpring05 {
	public static void main(String[] args) throws Exception {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		School school = applicationContext.getBean("school", School.class);
		int sid = school.getSid();
		System.out.println("sid:" + sid);
		Student stu = school.getStu();
		System.out.println(stu);
	}
}

4、使用SqEL注入

(1)Employee.java
package com.qf.chapter07.bean;

public class Employee {
	private int eid;
	private String name;
	private String department;

	public int getEid() {
		return eid;
	}

	public void setEid(int eid) {
		this.eid = eid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDepartment() {
		return department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	@Override
	public String toString() {
		return "Employee [eid=" + eid + ", name=" + name + ", department=" + department + "]";
	}
}
(2)Person.java
package com.qf.chapter07.bean;

public class Person {
	private int pid;
	private String name;

	public void setPid(int pid) {
		this.pid = pid;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "Person [pid=" + pid + ", name=" + name + "]";
	}
}
(2)applicationContext.xml(新增)
	<bean id="employee" class="com.qf.chapter07.bean.Employee">
		<property name="eid" value="1"></property>
		<property name="name" value="WangWu"></property>
		<property name="department" value="研发部"></property>
	</bean>
	<bean id="person" class="com.qf.chapter07.bean.Person">
		<property name="pid" value="#{employee.eid}"></property>
		<property name="name" value="#{employee.name}"></property>
	</bean>
(3)TestSpring06.java
package com.qf.chapter07.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.qf.chapter07.bean.Person;

public class TestSpring06 {
	public static void main(String[] args) throws Exception {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		Person person = applicationContext.getBean("person", Person.class);
		System.out.println(person);
	}
}

5、Bean的作用域

(1)applicationContext.xml(新增)
	<bean id="person01" class="com.qf.chapter07.bean.Person" />
	<bean id="person02" class="com.qf.chapter07.bean.Person" scope="prototype" />
(2)TestSpring07.java
package com.qf.chapter07.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.qf.chapter07.bean.Person;

public class TestSpring07 {
	public static void main(String[] args) throws Exception {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		// 获取id为person01的Person实例
		Person person01_1 = applicationContext.getBean("person01", Person.class);
		Person person01_2 = applicationContext.getBean("person01", Person.class);
		// 获取id为person02的Person实例
		Person person02_1 = applicationContext.getBean("person02", Person.class);
		Person person02_2 = applicationContext.getBean("person02", Person.class);
		// 判断两次获取的id为person01的Person实例是否相等
		System.out.println(person01_1 == person01_2);
		// 判断两次获取的id为person02的Person实例是否相等
		System.out.println(person02_1 == person02_2);
	}
}

6、Bean的生命周期

(1)Bean实例初始化之后执行指定方法

Bean01.java

package com.qf.chapter07.bean;

public class Bean01 {
	private String bid;
	private String name;

	public Bean01() {
	}

	public String getBid() {
		return bid;
	}

	public void setBid(String bid) {
		this.bid = bid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "Bean01 [bid=" + bid + ", name=" + name + "]";
	}

	public void init() {
		System.out.println("Bean的初始化完成,调用init()方法");
		System.out.println(this.toString());
	}
}

applicationContext.xml(新增)

	<bean id="bean01" class="com.qf.chapter07.bean.Bean01" init-method="init">
		<property name="bid" value="1"></property>
		<property name="name" value="xiaoqian"></property>
	</bean>

TestSpring08.java

package com.qf.chapter07.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring08 {
	public static void main(String[] args) throws Exception {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
	}
}

Bean02.java

package com.qf.chapter07.bean;

import org.springframework.beans.factory.InitializingBean;

public class Bean02 implements InitializingBean {
	private String bid;
	private String name;

	public Bean02() {
	}

	public String getBid() {
		return bid;
	}

	public void setBid(String bid) {
		this.bid = bid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "Bean02 [bid=" + bid + ", name=" + name + "]";
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("Bean的初始化完成,调用afterPropertiesSet()方法");
		System.out.println(this.toString());
	}
}

applicationContext.xml(新增)

	<bean id="bean02" class="com.qf.chapter07.bean.Bean02">
		<property name="bid" value="1"></property>
		<property name="name" value="xiaoqian"></property>
	</bean>

再次执行 TestSpring08.java

(2)Bean实例销毁之前执行指定方法

Bean03.java

package com.qf.chapter07.bean;

public class Bean03 {
	private String bid;
	private String name;

	public Bean03() {
	}

	public String getBid() {
		return bid;
	}

	public void setBid(String bid) {
		this.bid = bid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "Bean03 [bid=" + bid + ", name=" + name + "]";
	}

	public void close() {
		System.out.println("Bean实例即将销毁,调用close()方法");
		System.out.println(this.toString());
	}
}

applicationContext.xml(新增)

	<bean id="bean03" class="com.qf.chapter07.bean.Bean03" destroy-method="close">
		<property name="bid" value="1"></property>
		<property name="name" value="xiaoqian"></property>
	</bean>

TestSpring09.java

package com.qf.chapter07.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.*;

public class TestSpring09 {
	public static void main(String[] args) throws Exception {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		// 关闭容器,此时Bean实例将被销毁
		AbstractApplicationContext ac = (AbstractApplicationContext) applicationContext;
		ac.registerShutdownHook();
	}
}

Bean04.java

package com.qf.chapter07.bean;

import org.springframework.beans.factory.DisposableBean;

public class Bean04 implements DisposableBean {
	private String bid;
	private String name;

	public Bean04() {
	}

	public String getBid() {
		return bid;
	}

	public void setBid(String bid) {
		this.bid = bid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "Bean04 [bid=" + bid + ", name=" + name + "]";
	}

	@Override
	public void destroy() throws Exception {
		System.out.println("Bean实例即将销毁,调用destroy()方法");
		System.out.println(this.toString());
	}
}

applicationContext.xml(新增)

	<bean id="bean04" class="com.qf.chapter07.bean.Bean04">
		<property name="bid" value="1"></property>
		<property name="name" value="xiaoqian"></property>
	</bean>

再次执行 TestSpring09.java

三、注解的应用

1、修改配置文件 applicationContext.xml ,增加 beans 元素代码

(1)引入 context 约束
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xmlns:context="http://www.springframework.org/schema/context"
	   xmlns:p="http://www.springframework.org/schema/p"
	   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.xsd">
(2)增加配置注解扫描
	<context:component-scan base-package="com.qf.chapter07.bean"/>

2、新建Bean类

(1)Stu.java
package com.qf.chapter07.bean;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("stu")
public class Stu {
	@Value("001")
	private int sid;
	@Value("ZhangSan")
	private String name;
	private String age;
	private String course;

	public int getSid() {
		return sid;
	}

	public void setSid(int sid) {
		this.sid = sid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}

	public String getCourse() {
		return course;
	}

	public void setCourse(String course) {
		this.course = course;
	}

	@Override
	public String toString() {
		return "Stu [sid=" + sid + ", name=" + name + ", age=" + age + ", " + "course=" + course + "]";
	}
}
(2)Sch.java
package com.qf.chapter07.bean;

import javax.annotation.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component("sch")
@Scope(scopeName = "singleton")
public class Sch {
	@Value("005")
	private int sid;
	@Autowired
	@Qualifier("stu")
	private Stu stu;

	public int getSid() {
		return sid;
	}

	public void setSid(int sid) {
		this.sid = sid;
	}

	public Stu getStu() {
		return stu;
	}

	public void setStu(Stu stu) {
		this.stu = stu;
	}

	@Override
	public String toString() {
		return "Sch [sid=" + sid + ", stu=" + stu + "]";
	}

	@PostConstruct
	public void init() {
		System.out.println("Bean的初始化完成,调用init()方法");
	}

	@PreDestroy
	public void destroy() {
		System.out.println("Bean的初始化完成,调用destroy()方法");
	}
}

3、测试类 TestSpring10.java

package com.qf.chapter07.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.*;
import com.qf.chapter07.bean.Sch;

public class TestSpring10 {
	public static void main(String[] args) throws Exception {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		Sch sch = applicationContext.getBean("sch", Sch.class);
		System.out.println(sch);
		AbstractApplicationContext ac = (AbstractApplicationContext) applicationContext;
		ac.registerShutdownHook();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值