Java小白学习指南【day35】---Spring快速上手

1、spring概述

Spring是一个开源的轻量级控制反转(IOC)和面向切面编程(AOP)的容器框架(高内聚、低耦合)----> Spring集成了市面上几乎所有框架

注意事项:Spring底层原理:xml+dom4j+工厂设计模式+反射

IOC/DI 控制翻转/依赖注入

  • 把你的类交给Spring管理,Spring会负责对象的的创建,维护,[初始化,销毁]单利模式,多例模式

  • 向Spring取对象

在这里插入图片描述

2、spring入门

1、动态的web工程

2、导包,需要哪个就导哪个需要的包,不要一次性全导进来

3、 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
	<bean id="..." class="...">
	<!-- collaborators and configuration for this bean go here -->
	</bean>
</beans>

4、获取对象的方式(两种)

  • BeanFactory创建对象【3.2.4】过时

  • @Test
    	public void testBeanFactory() throws Exception {
          
          
    		//第一步:读取资源文件
    		Resource resource = new ClassPathResource("applicationContext.xml");
    		//第二步:拿到核心对象 BeanFactory
    		BeanFactory factory = new XmlBeanFactory(resource);
        }
    
  • ApplicationContext(注意要先导包)

@Test
	public void testApplicationContext() throws Exception {
    
    //这个要先导包
		//加载工程classpath下的配置文件实例化
		String conf = "applicationContext.xml";
		ApplicationContext factory = new ClassPathXmlApplicationContext(conf);
    }

上述两种获取对象的区别

BeanFactory与ApplicationContext区别和联系

  • ApplicationContext接口 extends BeanFactory接口 子对父有拓展,功能上来说,父更加强大一下

  • BeanFactory:懒加载模式,需要的时候才创建对象

  • ApplicationContext:迫切加载,管你需要还是不需要,上来就给搞对象

3、依赖注入

1、xml注入(必须有对应的setter方法)

2、注解注入(@Autowired为Spring提供的注解,需要导入包)

在这里插入图片描述

4、spring及细节

1、spring测试

需要先进性导包

*spring-test-4.1.2**.RELEASE.jar* *–* *测试包*

*spring-aop-4.1.2.RELEASE.jar* *–* *AOP包***

/**
 * @author wlk
 *可以通过注解来启动spring
 */
@RunWith(SpringJUnit4ClassRunner.class)//是在配置文件中设置value值
@ContextConfiguration("classpath:applicationContext.xml")//一般就使用这个
//@ContextConfiguration("/cn/xxxx/springtest/SpringTest-context.xml")
//@ContextConfiguration
/*还有多种写法
 * 2、写在当前包下时@ContextConfiguration("\cn\xxxx\springtest\SpringTest-context.xml")  包路劲
 * 3、@ContextConfiguration     默认在当前包中   测试类名-context.xml
 * */
public class SpringTest {
    
    
	//自动注入,会从spring中找对应的bean
	@Autowired
	Date date;
	@Autowired
	HelloBean hbBean;
	@Test
	public void testSpring() throws Exception {
    
    
		System.out.println(date);
		System.out.println(hbBean);
	}

}

2、配置细节

/**
 * @author wlk
 *细节配置  init-method指定初始化方法,destroy-method指定销毁方法 
 *lazy-init指定懒加载模式,懒模式就是需要对象的时候才会去加载
 *scope设置单例或者多例模式(默认为单例)
 *singleton单例/prototype多例
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class DetailBeanTest {
    
    
	@Autowired
	ApplicationContext context;

	@Test
	public void testDetail() {
    
    
		System.out.println(context);
		 DetailBean bean1 = context.getBean("detail",DetailBean.class);
		// DetailBean bean2 = context.getBean("detail",DetailBean.class);
		 System.out.println(bean1);
		// System.out.println(bean2);
	}
}

5、三层架构中使用spring

1、dao+service

  • Dao 写好Dao 放入到applicationContext.xml中 交给了Spring管理 并采用SpringTest测试

  • Service 写好service

    • 放入到applicationConxtext.xml中交给Spring管理 并采用SpringTest测试
    • 注入Dao
<!-- 4、三层结构在spring中的使用 -->
	<bean id="daoimpl" class="cn.xxxx.dao.impl.UserDaoImpl"></bean>
	<bean id="serviceimpl" class="cn.xxxx.service.impl.UserServiceImpl">
		<property name="dao" ref="daoimpl"></property>
	</bean>

2、controller

目标是:在tomcat启动的同时,启动spring,获取到service层的对象

1、想要从service层获取到对象
2、在tomcat加载的时候,spring也进行注入,就需要加入监听
3、通过获取Spring容器取得 ,获得里面的service对象

在web.xml配置监听

<!-- 加入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字段

//执行Servlet之前,执行初始化方法,去Spring中,获取到UserServiceImpl的对象,并赋值给usi;
	@Override
	public void init() throws ServletException {
    
    
		//上下文对象
		ServletContext sc = this.getServletContext();
		//Spring容器对象
		WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(sc);
			usi = context.getBean("service", UserServiceImpl.class);
	}

猜你喜欢

转载自blog.csdn.net/WLK0423/article/details/110875032
今日推荐