Spring生命周期
- 生命周期:
创建方法init
销毁方法destory
普通方法service - 属性
init-method当该对象初始化的时候该方法会自动执行
destory-method 当该对象即将销毁的时候会自动调用该方法 - 测试
context。close()关闭容器
创建方法
public void init(){
//初始化
System.out.println("哇哇哇,我诞生了");
}
public void eat(){
//普通方法
System.out.println("哈哈,我能吃饭了");
}
public void destory(){
//销毁方法
System.out.println("呜呜,我没了");
}
<bean id="person6" class="com.wxx.domain.Person"
init-method="init"
destroy-method="destory"
/>
测试方法
//spring生命周期
@Test
public void test05(){
Person person = (Person)context.getBean("person6");
person.eat();
person.destory();
}
Spring依赖注入DI-set方法
- 什么是依赖注入
DI(dependency injection)依赖注入
含义:就是给对象的属性设置值
set方法是给对象设置值
构造方法是给对象初始化的时候设置值 - property标签
如果让spring调set方法,前提条件是类中必须有set的方法
name:代表的是set方法去掉set,首字母小写setName Name name
value:基本类型或字符串类型的值,具体给属性设置用的
ref(引用):引用对象的id,作为一个对象类型注入
Spring依赖注入-给复杂类型注入
- 什么是复杂类型
简单的是基本类型与字符串
Array数组 List集合 Map集合 Set集合 Properties集合 - 如何给这些属性设置值
使用对应的子标签 array list map set props
//集合类型属于复杂类型
private String[] arr; //女朋友们
public void setArr(String[] arr) {
this.arr = arr;
}
public String[] getArr() {
return arr;
}
//List
private List<String> list;//前女朋友们
public void setList(List<String> list) {
this.list = list;
}
//set
private Set<String> set;//前女朋友们
public void setSet(Set<String> set) {
this.set = set;
}
//map
private Map<String,String> map;//前女朋友们
public void setMap(Map<String, String> map) {
this.map = map;
}
//properties
private Properties properties;//前女朋友们
public void setProperties(Properties properties) {
this.properties = properties;
}
public Person() {
}
applicationContext.xml
<bean id="person7" class="com.wxx.domain.Person">
<property name="name" value="jack"/>
<property name="arr">
<array>
<value>rose</value>
<value>rose</value>
<value>rose</value>
</array>
</property>
<property name="list">
<list>
<value>rose1</value>
<value>rose1</value>
<value>rose1</value>
</list>
</property>
<property name="set">
<set>
<value>rose3</value>
<value>rose3</value>
<value>rose3</value>
</set>
</property>
<property name="map">
<map>
<entry key="1000" value="rose1"/>
<entry key="1001" value="rose2"/>
<entry key="1002" value="rose3"/>
</map>
</property>
<property name="properties">
<props>
<prop key="10010">rose1</prop>
<prop key="10086">rose2</prop>
<prop key="110">rose3</prop>
</props>
</property>
</bean>
Spring依赖注入-xml方式Dao
- Test
//依赖注入 给复杂类型注入xml方式
@Test
public void test09(){
PersonService personService = (PersonService) context.getBean("personService");
Person p = new Person();
p.setUsername("jack");
p.setPassword("12345");
boolean flag=personService.login(p);
log.debug(flag+"");
}
- PersonService
public class PersonService {
private static final Logger log= LoggerFactory.getLogger(PersonService.class);
//private PersonDao personDao = new PersonDao();
private PersonDao personDao ;
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
public boolean login(Person p) {
log.debug(p+" login");
Person person = personDao.find(p);
if(person==null) {
return false;
}else{
return true;
}
}
}
- PersonDao
public class PersonDao {
public Person find(Person p) {
if("jack".equals(p.getUsername())&&"12345".equals(p.getPassword())){
return p;
}else{
return null;
}
}
}
- applicationContext.xml
<bean id="personService" class="com.wzx.service.PersonService">
<property name="personDao" ref="personDao"/>
</bean>
<bean id="personDao" class="com.wzx.dao.PersonDao">
</bean>
Spring依赖注入-注解创建对象
- 当遇到对象较多的时候,开启注解扫描
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--
使用注解方式进行创建对象
1.开启注解扫描
含义:开启注解扫描,指定了 base-package 扫描指定的包,扫描包与子包中所有的类
查看类上是否有指定的注解, 如果类上有指定的注解,那么就创建给类对象,
放到spring容器中
-->
<context:component-scan base-package="com.wzx"/>
- 只有标记有注解的类,才会被创建对象且添加到ioc容器中
- 四个注解
//@Component //其他层
//@Repository //Dao层
//@Service //Service层
@Controller("xxx")//Controller层
public class MyClass{
}
- 注解没有配置id,但是默认是myClass
Spring依赖注入-注解实现注入
- 注入是什么?
查找之后,进行赋值 - 三种注入方式
@Autowired
或者
@Autowired
@Qualifier(“bean的id”)
》2
@Value("#{bean的id}")
》3
@Resource(name=“bean的id值”)
@Service
public class PersonService {
//private PersonDao personDao = new PersonDao();
//第一种:@Autowired或者 @Autowired和@Qualifier("bean的id")搭配
//第二种:@Value("#{bean的id}")
//第三种:@Resource(name="bean的id值")
@Autowired
PersonDao personDao ;
}