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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--
创建bean的三种方式
使用默认构造函数: id, class
普通工厂中的方法创建对象,并存入spring容器: id, class, factory-bean, factory-method
普通工程中的静态方法创建对象,并存入spring容器:id, class, factory-method
bean对象的作用范围
scope: 指定bean的作用范围
singleton: 单例(默认值)
propotype:多例
request:作用于web应用的请求范围
session:作用于web应用的会话范围
global-session:作用于集群环境的会话范围(全局会话范围),当不是集群环境时,它就是session
bean对象的生命周期
单例对象(生命周期和容器相同)
出生:容器创建
活着:容器在,对象活着
死亡:容器销毁,对象销毁
多例对象
出生:使用时创建
活着:对象只要在使用就一直活着
死亡:当对象长时间不用,且没有别的对象引用时,由Java的垃圾回收器回收
-->
<!--
依赖注入:
注入的数据有三类
基本类型和String
其他bean类型(在配置文件中或者注解配置过的bean)
复杂类型/集合类型
注入的方式有三种
使用构造函数提供
construstor-arg标签:
type: 数据类型
index:指定参数索引(0开始)
name:指定构造函数参数名称
value:基本数据类型和String类型的数据
ref:用于指定其他的bean数据类型。它指的就是在spring的Ioc核心容器中出现过的bean对象
使用set方法
property标签:
name: 用于指定注入时所调用的set方法名称
value: 基本数据类型和String类型的数据
ref: 用于指定其他的bean数据类型。它指的就是在spring的Ioc核心容器中出现过的bean对象
使用注解提供
-->
<!--
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl" >
<property name="myStr">
<array>
<value>1</value>
<value>1</value>
<value>1</value>
<value>1</value>
</array>
</property>
<property name="myList">
<list>
<value>1</value>
<value>1</value>
<value>1</value>
</list>
</property>
<property name="mySet">
<set>
<value>1</value>
<value>1</value>
</set>
</property>
<property name="myMap">
<map>
<entry key="12" value="234"></entry>
<entry key="12s" value="234"></entry>
</map>
</property>
<property name="myProps">
<props>
<prop key="12321">sfd</prop>
</props>
</property>
</bean>
-->
<!-- 告知spring在创建容器时要扫描的包,配置所需要的标签不是在beans的约束中,而是一个名称为context名称空间和约束中 -->
<context:component-scan base-package="com.itheima"></context:component-scan>
</beans>
实现类:
package com.itheima.service.impl;
import com.itheima.dao.IAccountDao;
import com.itheima.factory.BeanFactory;
import com.itheima.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
import java.lang.reflect.Array;
import java.util.*;
/**
* 账户的业务层实现类
*
* 创建对象的注解: 把这个类作为一个控制器加载到Spring的Bean工厂
* Component CCSR
* Controller(跟Component一样) 一般用于表现层
* Service(跟Component一样) 一般用于业务层
* Repository(跟Component一样) 一般用于持久层
* 明确提供的三层使用注解,三层对象更加清晰
*
* 注入数据的注解
* Autowired
* 作用:自动按照类型注入,只要容器中有唯一的一个bean对象类型和注入的变量类型匹配,就可以注入成功。
* Qualifier
* value:用于指定注入bean的id
* 不能单独使用,跟Autowired一起使用
* Resource
* 直接按照bean的id注入,可以独立使用
* 属性:name用于指定bean的id
* 以上三个只能注入bean类型的数据,基本类型和String类型使用Value,集合类型只能用xml来实现。
* Value
* 基本类型和String类型
* 只能使用xml进行注入
* value: 指定数据的值,它可以使用spring中的SpEL(el表达式)
* ${表达式}
* 改变作用范围的注解
* Scope
* bean的作用范围
* value: 范围取值,singleton, prototype
* 生命周期相关的注解
* PreDestory:被@PreDestroy修饰的方法会在服务器卸载Servlet的时候运行,并且只会被服务器调用一次,类似于Servlet的destroy()方法。
* 被@PreDestroy修饰的方法会在destroy()方法之后运行,在Servlet被彻底卸载之前
*
* PostConstruct: 指定顺序:Constructor(构造方法) -> @Autowired(依赖注入) -> @PostConstruct(注释的方法),
* PostConstruct该注解被用来修饰一个非静态的void()方法。被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,
* 并且只会被服务器执行一次。PostConstruct在构造函数之后执行,init()方法之前执行。
*/
@Component("accountService") // 把这个类作为一个控制器加载到Spring的Bean工厂
public class AccountServiceImpl implements IAccountService {
private String[] myStr;
private List<String> myList;
private Set<String> mySet;
private Map<String, String> myMap;
private Properties myProps;
// 直接按照bean的id注入,依赖注入
// 方式一
// @Autowired
// @Qualifier(value = "accountDao")
// 方式二
@Resource(name = "accountDao")
private IAccountDao iAccountDao;
@PostConstruct
public void init() {
System.out.println("init");
}
@PreDestroy
public void destory() {
System.out.println("destory");
}
public void saveAccount() {
iAccountDao.saveAccount();
}
}
package com.itheima.dao.impl;
import com.itheima.dao.IAccountDao;
import org.springframework.stereotype.Repository;
@Repository("accountDao")
public class AccountDaoImpl implements IAccountDao {
public void saveAccount() {
System.out.println("dao save");
}
}
package com.itheima.ui;
import com.itheima.dao.IAccountDao;
import com.itheima.service.IAccountService;
import com.itheima.service.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 模拟一个表现层,用于调用业务
*/
public class Client {
public static void main(String[] args) {
/**
* ApplicationContext的三个实现类:(立即加载)
* AnnotationConfigApplicationContext
* ClassPathXmlApplicationContext 加载类路劲下的配置文件
* FileSystemXmlApplicationContext 加载磁盘下任意路径文件(必须有访问权限)
* BeanFactory:(延迟加载)
*/
// IOC控制反转,DI依赖注入
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
IAccountService accountService = ac.getBean("accountService", IAccountService.class);
IAccountService accountService2 = ac.getBean("accountService", IAccountService.class);
ac.close();
}
}
接口:
package com.itheima.dao;
public interface IAccountDao {
void saveAccount();
}
package com.itheima.service;
/**
* 账号业务层的接口
*/
public interface IAccountService {
/**
* 模拟保存账户
*/
void saveAccount();
}