标签:spring
Spring是分层的JavaSE/EE full-stack(一站式) 轻量级开源框架IOC:控制反转.
IOC:控制反转:将对象的创建权,由Spring管理.
DI:依赖注入:在Spring创建对象的过程中,把对象依赖的属性注入到类中
ApplicationContext 应用上下文,加载Spring 框架配置文件
ApplicationContext类继承了BeanFactory
BeanFactory在使用到这个类的时候,getBean()方法的时候才会加载这个类
ApplicationContext类加载配置文件的时候,创建所有的类
ApplicationContext对BeanFactory提供了扩展
Window--->xml catalog--->add 找到schema的位置 ,将复制的路径 copy指定位置,选择schema location.
https://repo.spring.io/release/org/springframework/spring/
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
开发的日志记录的包:
com.springsource.org.apache.commons.logging-1.1.1.jar --- 用于整合其他的日志的包(类似Hibernate中slf4j)
com.springsource.org.apache.log4j-1.2.15.jar
2.创建Spring的配置文件
在src下创建一个applicationContext.xml
引入XML的约束:
找到xsd-config.html.(*****)引入beans约束:
<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">
3.在配置中配置类
<!--通过bean标签 设置类的信息,id为类取个标识 -->
<bean id="userService" class="cn.spring.demo1.HelloServiceimpl">
<!-- 使用<property>标签注入属性 values是普通值 ref是对象-->
<property name="info" value="依赖注入" />
</bean>
完整的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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--通过bean标签 设置类的信息,id为类起个标识 -->
<bean id="userService" class="cn.spring.demo1.HelloServiceimpl">
<!-- 使用<property>标签注入属性 values是普通值 ref是对象-->
<property name="info" value="依赖注入" />
</bean>
</beans>
package cn.spring.demo1;
public interface HelloService {
public void sayHello();
}
5.HelloServiceimpl..java 实现类
package cn.spring.demo1;
/**
* @author NOP
*
*/
public class HelloServiceimpl implements HelloService {
private String info;
public void setInfo(String info) {
this.info = info;
}
public void sayHello() {
// TODO Auto-generated method stub
System.out.print("hello spring..."+info);
}
}
package cn.spring.demo1;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
public class SpringTest1 {
@Test
// 传统方式
public void demo1() {
// 造成程序紧密耦合
HelloService helloservice = new HelloServiceimpl();
helloservice.sayHello();
}
//加载classpath:new ClassPathXmlApplicationContext("applicationContext.xml");
@Test
public void demo2() {
//创建一个工厂类
//加载classpath:new ClassPathXmlApplicationContext("applicationContext.xml");
ApplicationContext applicationcontext = new ClassPathXmlApplicationContext(
"applicationContext.xml");// 如果不写会去web-inf下去找applicationContext.xml
HelloService helloservice = (HelloService)applicationcontext.getBean("userService");
helloservice.sayHello();
}
//加载磁盘路径: new FileSystemXmlApplicationContext("applicationContext.xml");
@Test
public void demo3( ) {
//创建一个工厂类
//加载磁盘路径: new FileSystemXmlApplicationContext("applicationContext.xml");
ApplicationContext applicationcontext = new FileSystemXmlApplicationContext(
"applicationContext.xml");// 如果不写会去web-inf下去找applicationContext.xml
HelloService helloservice = (HelloService)applicationcontext.getBean("userService");
helloservice.sayHello();
}
//BeanFactory模式
@Test
public void demo4(){
//ClassPathResource
//BeanFactory beanFactory= new XmlBeanFactory(new ClassPathResource("applicationContext.xml")); :加载classpath下面配置文件.
BeanFactory beanFactory= new XmlBeanFactory(new FileSystemResource("applicationContext.xml"));//加载磁盘下的配置文件
HelloService helloservice = (HelloService)beanFactory.getBean("userService");
helloservice.sayHello();
}
}
标签:spring
原文地址:http://blog.51cto.com/4534309/2107550