老规矩了分为两步,导jar包和配置文件
1.导入的jar包
2.配置xml文件(applicationContext.xml)
配置文件中注入依赖方式又分为两类
1)手动配置
①在xml文件中通过<bean>节点下配置
②在java代码中使用@Autowire或@Resource注解方式进行装配
(1)需要引入表头信息
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation下加入:
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
(2)xml中要先编写如下代码<context:annotation-config></context:annotation-config>
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config></context:annotation-config>
<bean id="t" class="com.bean.Teacher" init-method="st" p:tname="荆轲">
</bean>
<bean id="cl" class="com.bean.ClassInfo"></bean>
<bean id="sd" abstract="true">
<property name="cltitle" value="修道班"></property>
</bean>
<bean id="cl2" class="com.bean.ClassInfo" parent="sd"></bean>
</beans>
(3)在相应的类字段属性中进行注解
package com.bean;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
@Component
public class ClassInfo {
private String clid;
private String cltitle;
//使用@Resource进行注解
@Resource(name="t")
private Teacher teacher;
public String getClid() {
return clid;
}
public void setClid(String clid) {
this.clid = clid;
}
public String getCltitle() {
return cltitle;
}
public void setCltitle(String cltitle) {
this.cltitle = cltitle;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
public ClassInfo(String clid, String cltitle, Teacher teacher) {
super();
this.clid = clid;
this.cltitle = cltitle;
this.teacher = teacher;
}
public ClassInfo() {
// TODO Auto-generated constructor stub
}
}
2)自动装配
<bean id="..." class="..." auto>
二、bean的高级特性
1.bean的作用域
2.bean的生命周期
<!-- 定义抽象父类-->
<bean id="daoTemplate" abstract="true">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 继承父类-->
<bean id="stateDao" class="org. dao.impl.StateDaoHibernate"
parent="daoTemplate"/>
ApplicationContext app=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml","car.xml"});