文档版本 | 开发工具 | 测试平台 | 工程名字 | 日期 | 作者 | 备注 |
---|---|---|---|---|---|---|
V1.0 | 2016.06.21 | lutianfei | none |
Spring是分层的JavaSE/EE full-stack(一站式) 轻量级开源框架
SUN提供的EE的三层结构:web层、业务层、数据访问层(持久层,集成层)
Spring框架有对三层的一站式解决方案:
Spring的出现是为了取代EJB的臃肿、低效、脱离现实
IOC:(Inverse of Control 反转控制)
AOP : Aspect Oriented Programming 是 面向对象的功能延伸.不是替换面向对象,是用来解决OO中一些问题.
官方下载Spring 3.x 最新开发版本
Spring开发包 : spring-framework-3.2.0.RELEASE-dist.zip
spring-expression-3.2.0.RELEASE.jar
开发的日志记录的包:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- bean definitions here -->
</beans>
<!-- 通过一个<bean>标签设置类的信息,通过id属性为类起个标识. -->
<bean id="userService" class="cn.itcast.spring3.demo1.HelloServiceImpl"></bean>
@Test
// Spring开发
public void demo2() {
// 创建一个工厂类.
ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "applicationContext.xml");
HelloService helloService = (HelloService) applicationContext.getBean("userService");
helloService.sayHello();
}
HelloTest类中使用 HelloService类对象
IoC Inverse of Control 反转控制的概念,就是将原本在程序中手动创建HelloService对象的控制权,交由Spring框架管理,简单说,就是创建HelloService对象控制权被反转到了Spring框架
DI:依赖注入:在Spring创建对象的过程中,把对象依赖的属性注入到类中.
面向对象中对象之间的关系;
public class A{
private B b;
}
* 继承:is a
* 聚合:
* 聚集:
* 组合:
ApplicationContext对BeanFactory提供了扩展:
Eg:
@Test
public void demo4(){
// ClassPathResource FileSystemResource
BeanFactory beanFactory = new XmlBeanFactory(new FileSystemResource("applicationContext.xml"))</u>;
HelloService helloService = (HelloService) beanFactory.getBean("userService");
helloService.sayHello();
}
<!-- 默认情况下使用的就是无参数的构造方法. -->
<bean id="bean1" class="cn.itcast.spring3.demo2.Bean1"></bean>
<!-- 第二种使用静态工厂实例化 -->
<bean id="bean2" class="cn.itcast.spring3.demo2.Bean2Factory" factory-method="getBean2"></bean>
<!-- 第三种使用实例工厂实例化 -->
<bean id="bean3" factory-bean="bean3Factory" factory-method="getBean3"></bean>
<bean id="bean3Factory" class="cn.itcast.spring3.demo2.Bean3Factory"/>
public class PersonServiceFactory {
public PersonService createPersonService(){
return new PersonServiceImpl();
}
}
/**
* Bean的实例化的测试
*
* @author 姜涛
*
*/
public class SpringTest2 {
@Test
// 无参数的构造方法的实例化
public void demo1() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"applicationContext.xml");
Bean1 bean1 = (Bean1) applicationContext.getBean("bean1");
System.out.println(bean1);
}
@Test
// 静态工厂实例化
public void demo2() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"applicationContext.xml");
Bean2 bean2 = (Bean2) applicationContext.getBean("bean2");
System.out.println(bean2);
}
@Test
// 实例工厂实例化
public void demo3() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"applicationContext.xml");
Bean3 bean3 = (Bean3) applicationContext.getBean("bean3");
System.out.println(bean3);
}
scope属性 :
实际开发中主要使用 singleton,prototype
11 . 调用手动销毁方法 : 调用<bean destroy-method="customerDestroy"> 指定销毁方法 customerDestroy
案例:在CustomerService类的add方法之前进行权限校验
xml配置
<bean id="customerService" class="cn.itcast.spring3.demo4.CustomerServiceImpl" init-method="setup" destroy-method="teardown">
<property name="name" value="itcast"></property>
</bean>
<bean class="cn.itcast.spring3.demo4.MyBeanPostProcessor"></bean>
//这里不需要写id,因为这是由Spring容器自动调用的。
public interface CustomerService {
public void add();
public void find();
}
public class CustomerServiceImpl implements CustomerService, BeanNameAware,ApplicationContextAware,InitializingBean,DisposableBean {
private String name;
public void setName(String name) {
System.out.println("第二步:属性的注入.");
this.name = name;
}
public CustomerServiceImpl() {
super();
System.out.println("第一步:实例化类.");
}
public void add(){
System.out.println("添加客户...");
}
public void find(){
System.out.println("查询客户...");
}
public void setBeanName(String name) {
System.out.println("第三步:注入配置的类的名称"+name);
}
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
System.out.println("第四步:注入applicationContext"+applicationContext);
}
public void afterPropertiesSet() throws Exception {
System.out.println("第六步:属性设置后执行...");
}
public void setup(){
System.out.println("第七步:调用手动设置的初始化方法...");
}
public void destroy() throws Exception {
System.out.println("第十步:调用销毁的方法...");
}
public void teardown(){
System.out.println("第十一步:调用手动销毁方法...");
}
}
public class MyBeanPostProcessor implements BeanPostProcessor{
/**
* bean:实例对象
* beanName:在配置文件中配置的类的标识.
*/
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("第五步:初始化之前执行...");
return bean;
}
public Object postProcessAfterInitialization(final Object bean, String beanName)
throws BeansException {
System.out.println("第八步:初始化后执行...");
// 动态代理:
if(beanName.equals("customerService")){
Object proxy = Proxy.newProxyInstance(bean.getClass().getClassLoader(), bean.getClass().getInterfaces() , new InvocationHandler() {
// 调用目标方法的时候,调用invoke方法.
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
if("add".equals(method.getName())){
System.out.println("权限校验...");
Object result = method.invoke(bean, args);
//System.out.println(System.currentTimeMillis());
return result;
}
return method.invoke(bean, args);
}
});
return proxy;
}
return bean;
}
}
public class SpringTest4 {
@Test
// Bean完整的生命周期
public void demo1() {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"applicationContext.xml");
CustomerService customerService = (CustomerService) applicationContext.getBean("customerService");
customerService.add();
customerService.find();
applicationContext.close();
}
}
//结果如下:
第一步:实例化类
第二步:属性的注入
第三步:注入配置的类的名称 customerService
第四步:注入applicationContext org.springframework.context.support.ClassPathXmlApplicationContext@61baa894: startup date [Wed Jun 22 11:49:14 CST 2016]; root of context hierarchy
第五步:初始化之前执行...
第六步:属性设置后执行...
第七步:调用手动设置的初始化方法...
第八步:初始化后执行...
权限校验...
添加客户...
查询客户...
11:49:14,863 INFO ClassPathXmlApplicationContext:1042 - Closing org.springframework.context.support.ClassPathXmlApplicationContext@61baa894: startup date [Wed Jun 22 11:49:14 CST 2016]; root of context hierarchy
11:49:14,864 INFO DefaultListableBeanFactory:444 - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@64bfbc86: defining beans [customerService,test.spring3.demo4.MyBeanPostProcessor#0]; root of factory hierarchy
第十步:调用销毁的方法...
第十一步:调用手动销毁的方法...
IoC容器装配Bean 基于XML配置方式
<bean id="car2" class="cn.itcast.spring3.demo5.Car2">
<!-- <property>标签中name就是属性名称,value是普通属性的值,ref:引用其他的对象 -->
<property name="name" value="保时捷"/>
<property name="price" value="5000000"/>
</bean>
Spring2.5版本引入了名称空间p
引入名称空间:
xml:
<bean id="car2" class="cn.itcast.spring3.demo5.Car2" p:name="宝马" p:price="400000"/>
<bean id="person" class="cn.itcast.spring3.demo5.Person" p:name="童童" p:car2-ref="car2"/>
<bean id="car2" class="cn.itcast.spring3.demo5.Car2">
<property name="name" value="#{'大众'}"></property>
<property name="price" value="#{'120000'}"></property>
</bean>
<bean id="person" class="cn.itcast.spring3.demo5.Person">
<!--<property name="name" value="#{personInfo.name}"/>-->
<property name="name" value="#{personInfo.showName()}"/>
<property name="car2" value="#{car2}"/>
</bean>
<bean id="personInfo" class="cn.itcast.spring3.demo5.PersonInfo">
<property name="name" value="张三"/>
</bean>
<bean id="collectionBean" class="cn.itcast.spring3.demo6.CollectionBean">
<!-- 注入List集合 -->
<property name="list">
<list>
<value>童童</value>
<value>小凤</value>
</list>
</property>
<!-- 注入set集合 -->
<property name="set">
<set>
<value>杜宏</value>
<value>如花</value>
</set>
</property>
<!-- 注入map集合 -->
<property name="map">
<map>
<entry key="刚刚" value="111"/>
<entry key="娇娇" value="333"/>
</map>
</property>
<property name="properties">
<props>
<prop key="username">root</prop>
<prop key="password">123</prop>
</props>
</property>
</bean>
第一种写法:
第二种方法:
IoC容器装配Bean , 基于注解配置方式
Spring2.5 引入注解去定义Bean
Xml:头文件中加入context路径:xmlns:context=http://www.springframework.org/schema/context
<?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">
<!-- bean definitions here -->
</beans>
Spring提供对JSR-250中定义@Resource标准注解的支持@Resource和@Autowired注解功能相似
下面两个例子等价
@Autowired
@Qualifier("userDao")
private UserDao userDao;
@Resource(name="userDao")
private UserDao userDao;
Spring3.0以JavaConfig为核心,提供使用Java类定义Bean信息的方法
之前已经通过 component-scan标签 对配置类进行了扫描,故这里不需要再进行手动配置扫描了。
@Configuration
public class BeanConfig {
@Bean(name="car")
public Car showCar(){
Car car = new Car();
car.setName("长安");
car.setPrice(40000d);
return car;
}
@Bean(name="product")
public Product initProduct(){
Product product = new Product();
product.setName("空调");
product.setPrice(3000d);
return product;
}
}
正常整合Servlet和Spring没有问题的,但是每次执行Servlet的时候加载Spring配置以及加载Spring环境
解决办法:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
WebApplicationContext applicationContext = (WebApplicationContext) getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
eg:
web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name></display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class>cn.itcast.servlet.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/userServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
public class UserServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"applicationContext.xml");*/
WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
UserService userService = (UserService) applicationContext
.getBean("userService");
userService.sayHello();
}
public class UserService {
public void sayHello(){
System.out.println("Hello Spring web...");
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class SpringTest {
@Autowired
private UserService userService;
@Test
public void demo1(){
userService.sayHello();
}
}