前言:上篇关于Spring的文章介绍了一些Spring的基本知识,此篇文章主要分享一下如何配置Spring环境,如何注入等。
Spring项目构建
导入Spring相关JAR包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.4.RELEASE</version>
</dependency>
准备Spring配置文件
在classpath的根目录下新建一个applicationContext.xml配置文件,文件名可以自定义,但是通常使用applicationContext这个名字;
添加文档声明和约束(这个东西不需要记忆):
可以参考文档,中英文文档都可以;
spring-framework-4.1.2.RELEASE\docs\spring-framework-reference\pdf
可以参考资源中的资料;
可以百度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>
编写一个类
public class MyBean {
public void hello(){
System.out.println("hello spring...");
}
}
将编写的类交给Spring容器管理
Spring是一个容器,我们需要把我们的类交给Spring去管理。 因为,我们的测试是创建一个普通的类,然后再通过Spring帮我们把这个类的对象创建出来就算是成功了;
在配置文件中将这个Java类交给Spring管理。在applicationContext.xml中配置
<beans ...> <bean id="myBean" class="org.com.Spring._01_hello.MyBean"></bean> </beans>
元素和属性讲解:
bean元素:表示对象配置或注册标签;
id属性:这个bean对象在Spring容器中的唯一标识,也可以使用name,常用id(唯一特性),获取这个对象的时候就可以通过这个表示来获取;
class属性:对应对象所属类的完全限定名。注意这里可以是JDK自带的类,也可以是自己新建的类;
Spring容器实例化
Spring容器对象有两种:BeanFactory和ApplicationContext;
ApplicationContext继承自BeanFactory接口,拥有更多的企业级方法,推荐使用该类型;
BeanFactory
BeanFactory是一个接口,可以通过其实现类XmlBeanFactory获取其实例。接口中有一个getBean()方法可以获取Spring容器中配置或注册的Bean对象;
@Test
public void testHelloSpring() throws Exception {
/**
*我们第一步是要启动框架,而启动框架则需要拿到Spring的核心对象
*/
//第一步:读取资源文件
Resource resource = new ClassPathResource("applicationContext.xml");
//第二步:拿到核心对象 BeanFactory
BeanFactory factory = new XmlBeanFactory(resource);
}
ApplicationContext
ApplicationContext的中文意思是"应用程序上下文",它继承自BeanFactory接口,除了包含BeanFactory的所有功能之外,在国际化支持、资源访问(如URL和文件)、事件传播等方面进行了良好的支持,被推荐为JavaEE应用之首选,可应用在Java APP与Java Web中;
加载工程classpath下的配置文件实例化
String conf = "applicationContext.xml";
ApplicationContext factory = new ClassPathXmlApplicationContext(conf);
获取对象方式
方式一:通过id直接拿到相应的Bean对象
//通过[xml]{.ul}中配置的id拿到对象
MyBean bean = (MyBean)factory.getBean("myBean");
System.out.println(bean);
方式二:通过id与对象的Class对象拿到Bean对象(推荐使用)
//通过id与对象的class拿到Bean对象
MyBean bean = factory.getBean("myBean",MyBean.class);
System.out.println(bean);
ApplicationContext与BeanFactory的区别
联系
ApplicationContext是BeanFactory的子类,拥有更多的功能与方法;
区别
ApplicationContext默认是在读取配置文件的时候就会根据配置创建Bean对象(迫切加载)。而BeanFactory是在使用的时候才进行对象的创建(懒加载/延迟加载)
Spring管理bean的方式
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd"
>
<bean id="myBean" class="com.ss._04di._01xml.MyBean">
</bean>
<bean id="otherBean" class="com.ss._04di._01xml.OtherBean"></bean>
</beans>
注解
配置扫描注解
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
"
>
<!--扫描组件:通过指定包,就是找到本包或者子孙包下面加了注解Component及@Controller等注解的类,把它交给spring管理
就相当于我们xml当中一个一个配置-->
<context:component-scan base-package="com.ss._04di._02annotation"></context:component-scan>
</beans>
加注解
@Component @Controller @ Service @Repository等注解,后面三个是前一个子注解。其实我们任意用一个都可以,但是为了代码可读性,最好分门别类的使用 。
Controller就用 @Controller 注解
Service就用@Service注解
dao/mapper用@Repository
Spring依赖注入
IoC是一种思想,它的一个重点是在系统运行中,动态的向某个对象提供它所需要的其他对象。这一点是通过DI(Dependency Injection,依赖注入)来实现的;
Spring中的对象都是由Spring进行统一管理,但是在对象中还存在属性,这些属性实际上引用的也是别的对象,那么这些对象也是由Spring来管理的;
在实际使用时,我们需要给Spring中对象的属性字段赋值,这称为依赖注入DI(Dependency Injection);
依赖注入又分为xml注入和注解注入;
XML注入
Bean定义:
public class MyBean{
private OtherBean otherBean;
public void hello(){
otherBean.hello();
}
public void setOtherBean(OtherBean otherbean){
this.OtherBean = OtherBean
}
}
public class OtherBean{
public void hello(){
System.out.println("otherbean hello");
}
}
xml定义
//xml配置:
<bean id="otherBean" class="org.com.bean.OtherBean"></bean>
<bean id="myBean" class="org.com.bean.MyBean">
<property name="otherBean" ref="otherBean"></property>
</bean
测试
//测试:main方法测试
//加载工程classpath下的配置文件实例化
String conf = "applicationContext.xml";
ApplicationContext factory = new ClassPathXmlApplicationContext(conf);
注解注入
使用@Autowired
@Component
public class ServiceBean {
@Autowired
private DaoBean1 daoBean;
public void sayHello(){
System.out.println("hello spring!!!!");
daoBean.hello();
}
}
@Component
public class DaoBean1 {
public void hello(){
System.out.println("hello DaoBean1!!!!!");
}
}
@Component
public class DaoBean {
public void hello(){
System.out.println("hello DaoBean!!!!!");
}
}
使用@Resource
public class MyBean{
@Resource //默认按照名字匹配【名字对了,类型也必须一致】,然后按照类型匹配
//@Resource(name="otherBean1")//指定Bean的名称
private OtherBean otherBean;
public void hello(){
otherBean.hello();
}
}
public class OtherBean{
public void hello(){
System.out.println("otherbean hello");
}
}
写在最后
Spring的依赖注入,翻转控制,面向切面编程是其最主要的特点与优势,后续博主也会继续分享相关的文章。博主小,中,大厂均有面试经历,每日分享JAVA全栈知识,希望能与大家共同进步。