One: to build a foundation
Need: eclipse, spring plug (to confirm the version number and download the corresponding plug-See: https: //blog.csdn.net/a1150499208/article/details/87988392)
Two: Create a spring project and configuration xml file
1. Download spring relevant jar package
A, loggingjar package Download: https://commons.apache.org/proper/commons-logging/download_logging.cgi , here to download version 1.2.
b, Springjar package Download: http://repo.springsource.org/libs-release-local/org/springframework/spring/ , here to download the version 4.3.10.
2. The first spring created
a, create a java project
b, to project below the jar package Sword
① lib folder is created and copied to the following jar package folder
② Select the copy, right click on the jar package BuildPath - Add to BuildPath
c, create a file SpringXML
① Select the src directory - Right-click on the selected item Other ··· - find the open spring node, appears below:
② Select Spring Bean Configuration File child nodes, click Next, appears below:
③ In the Filename text box, enter the file name of the configuration file (here applicationContext.xml) Click Next, appears below:
④ Selection beans (object instantiation) and context (automatic scanning) and click Finish
Three: spring's bean tag and automatically scan
a、在src的com.zzj.vo中创建一个类Person,在com.zzj.test中创建另一个类Test
b、创建一个spring的xml文件
c、bean的标签属性
①id和name
为bean标签添加id和name属性都可以通过调用getBean(id属性值或name某个属性值)方法获取到SpringIOC容器中的对象;但需要注意以下几点:
1、id属性值只能定义一个标识,而name属性值可以使用分号(“;”)、空格(“ ”)或逗号(“,”)分隔定义多个,如下代码:
1 <bean id="a" name="b,c" class="com.zzj.test.Person"></bean>
那么可通过如下方法获得Spring IOC容器的对象:
//将xml里配置好的类实例化 ApplicationContext context = new ClassPathXmlApplicationContext("test.xml"); Object obj1 = context.getBean("a"); Object obj2 = context.getBean("b"); Object obj3 = context.getBean("c"); System.out.println(obj1 +"----"+obj2 +"----"+obj3);
2、id和name不可以重复
3、bean标签中可以没有id和name属性,此时可以通过调用getBean(Class对象或全名)方法获取容器中的对象如下所示:
System.out.println(context.getBean(Person.class));
②懒加载
默认情况下,IOC容器初始化时便会把bean实例化,可以通过如下两种方式实现bean实例化懒加载(即在使用该bean对象时才实例化,这样可以节省系统资源):
a、在beans标签中添加default-lazy-init="true",则在该标签中配置的所有bean将实现懒加载;
b、在对应的bean标签中添加lazy-init="true",则该bean将实现懒加载,该属性没有继承性;
注意:bean标签中设置lazy-init的优先级高于在beans标签中设置default-lazy-init
③作用域
scope="singleton":默认值,IoC容器中只存在一个Java类对象,此时Java对象为单例,即每次从IoC容器获取的Java对象都是同一个;
scope="prototype":每次从IoC容器获取Java对象都是新的对象;
scope="request":每次HTTP请求都会创建一个新的Bean,该作用域只适用于WebApplicationContext环境;
scope="session":每次有新的会话都会创建一个新的Bean,该作用域只适用于WebApplicationContext环境;
d、自动扫描(命名空间中勾选context,引入spring-aop的jar包)
1、xml文件中输入如下代码
<context:component-scan base-package="com.zzj.test"></context:component-scan>
即可自动扫描对应位置的文件中的类
2、base-package标签属性属性值:
a、Spring 容器将会扫描该属性值指定包及其子包中的所有类;
b、该属性值支持*通配符,例如“com.lq.*.imp”表示扫描诸如com.lq.book.imp包及其子包中的类;
c、当需要扫描多个包时, 使逗号分隔;
d、resource-pattern标签属性可以指定Spring容器仅扫描特定的类而非基包下的所有类,比如resource-pattern="/*.class"表示只扫描基包下的类,基包的子包不会被扫描。
3、Sprng容器通过context:component-scan标签扫描其base-package标签属性值指定的包及其子包内的所有的类并实例化被@Component、@Repository、@Service或@Controller等注解所修饰的类。
@Component:基本注解
@Respository:持久层(一般为dao层)注解
@Service:服务层或业务层(一般为service层)注解
@Controller:控制层(一般为controller层)注解
默认情况下Spring依据默认命名策略为通过注解实例化的对象命名:类名第一个字母小写. 也可以在注解中通过@Component、@Repository、@Service或@Controller注解的value属性标识名称。