Spring的认识
Spring是一个DI/IOC与AOP的Bean[对象]容器框架
IOC 控制翻转 以前是操作者自己控制 而现在使用Spring框架 会把类交给Spring管理 负责对象的创建
DI 依赖注入:通过配置xml或者注解的方式为对象字段赋予值
Spring入门
第一个Spring
准备工作:
1.创建项目(动态web项目)
2.导包
3.导入Spring配置文件(通常使用applicationContext.xml名字 不用自己写)
4.编写一个类
public class HelloBean {
HelloBean(){
System.out.println("对象被创建");
}
}
5.把类交给Spring管理
<beans>
<bean id="myBean" class="cn.itsource._01_hello.MyBean"></bean>
</beans>
id就是唯一的名称(咱们需要通过这个名字拿到这个bean对象)
class需要通过哪个类创建对象(Spring会自己通过反射帮你把这个类创建出来) 写全限定名
启动框架
有两种方式 BeanFactroy
和ApplicationContext
都是需要用他们
接口的实现类
public class BeanTest {
@Test
public void testBeanFactory() throws Exception {
//1.启动对象
//读取资源文件
ClassPathResource resource = new ClassPathResource("applicationContext.xml");
//2.从spring中获取对象
//拿到核心对象
BeanFactory factory = new XmlBeanFactory(resource);
Object bean = factory.getBean("helloBean");
//前一个参数表示bean的id对应值 后面一个参数是取对象的 类名.class 可以获取到正确的类型
HelloBean bean2 = factory.getBean("helloBean", HelloBean.class);
}
@Test
public void testContext() throws Exception {
//1.启动容器
//拿到核心对象
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.工厂对象,根据applicationContext.xml 配置bean的id 获得Bean对象
HelloBean bean = context.getBean("helloBean", HelloBean.class);
Date bean2 = context.getBean("date",Date.class);
System.out.println(bean2);
}
}
两者的不同:
applicationContext接口继承BeanFactory 接口
子类型一般会对父类型做拓展,功能上比父类强
1.BeanFactory: 懒加载模式: 去Spring对象那里要的时候,Spring才开始创建对象
2.ApplicationContext:迫切加载: 管你需不需要先给创建一个对象
获取bean的三种方式
- 根据名称
ac.getBean("hello")
需要强转 - 根据类型
ac.getBean(HelloBean.class)
相同类型只能唯一 - 名称+类型
ac.getBean("hello", HelloBean.class)
依赖注入
1.配置相应的bean,并且注入属性
<!-- 2.依赖注入 -->
<bean id="mybean" class="cn.itsource._02beandi.MyBean">
<!-- 普通值value -->
<property name="name" value="阿宾"></property>
<property name="hb" ref="helloBean"></property>
</bean>
2.在Bean提供属性(setXxx 才是Bean对象的属性) //必须提供set方法 才能提供相应属性
public class UserServiceImpl implements IUserService {
UserDaoImpl dao;
//用依赖注入给dao对象 需要有set方法 setXxxx才是属性
public void setDao(UserDaoImpl dao) {
this.dao = dao;
}
@Override
public void eat() {
dao.add();
}
}
Spring测试功能
步骤
1.导入相应的包
- test :Spring的测试包
- aop :Spring面向切面包(测试需要用到)
2.编写测试类和方法
注解
@RunWith(SpringJUnit4ClassRunner.class)
:spring与测试一起启动
@ContextConfiguration("classpath:applicationContext.xml")
:获取配置文件
- 如果没有写路径,默认会到当前位置找到
类名-Context.xml
的文件 - 加上classpath: 会到资源根路径下找文件
- 不加classpath: 相对路径下找文件
@Autowired
自动注入 会从spring找到对应bean 会赋予对象
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml") /*默认到编译根路径查找Spring配置文件applicationContext.xml(较常用)*/
//@ContextConfiguration("classpath:cn/itsource/_03springtest/applicationContext.xml") //去编译路径找
//@ContextConfiguration("/cn/itsource/_03springtest/applicationContext.xml") //包的路径
//@ContextConfiguration //默认在当前包中 测试类名-context.xml(必须同包 名称一定要注意)
public class SpringTest {
@Autowired //自动注入 会从spring找到对应bean 会赋予对象
Date date;
@Autowired
HelloBean hb;
@Test
public void testName() throws Exception {
System.out.println(date);
System.out.println(hb);
}
}
配置细节
Spring中Bean的作用域
一般默认使是单例 急加载 也可以在xml自己配置
scope=“prototype” :就可以变成多例
lazy-init=“false”:false就是懒加载 true 就是急加载
<!-- 3、BeanDetail 配置细节 -->
<bean id="db" class="cn.itsource._04beandetail.DetailBean" scope="prototype"
lazy-init="false" init-method="init" destroy-method="destroy">
</bean>
生命周期的认识
被创建(执行构造方法)-初始化-执行方法-销毁
public class DetailBean {
public DetailBean(){
System.out.println("被创建");
}
//初始化方法
public void init(){
System.out.println("初始化方法");
}
//销毁方法
public void destroy(){
System.out.println("销毁方法");
}
}
在xml配置生命周期
<!-- 3、BeanDetail 配置细节 -->
<bean id="db" class="cn.itsource._04beandetail.DetailBean"
lazy-init="false" init-method="init" destroy-method="destroy">
</bean>
init-method=“init”:初始方法
destroy-method=“destroy”:销毁方法 后面写方法名称
三层架构中使用Spring
1.导包
2.准备好各层结构 并交给applicationContext.xml
管理
3.在web.xml配置spring监听程序 tomcat已启动 spring就启动
<!--配置Spring监听程序:在tomcat启动的时候 启动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>
当Servlet 初始化方法中,获取Service 对象并赋值给Servlet字段
@WebServlet("/user")
public class UserServlet extends HttpServlet{
UserServiceImpl service;
@Override
protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
System.out.println("执行成功");
service.eat();
}
@Override
public void init() throws ServletException {
//得到spring容器对象
WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
//获取UserServiceImpl对象 并把对象赋给service
service = context.getBean("userserviceimpl",UserServiceImpl.class);
System.out.println("初始化方法");
}
}
在Servlet中 先执行init方法获取对象 并赋值给字段 避免重复获取对象