Home >Java >javaTutorial >Detailed introduction to JAVAEE—spring
Positive It’s because the nature of the spring framework is that of a container.
The objects installed in the container have their functions. So it can be a one-stop shop.
Not only does it not exclude other frameworks, it can also help other frameworks Management objects.
aop support, ioc idea, spring jdbc, aop transaction, junit test support
Log package: com.springsource.org.apache.commons.logging-1.1.1.jar
Optional: com.springsource.org.apache.log4j-1.2.15.jar (the old version needs to be imported, and the import can guarantee that it will run)
public class User {private String name;private Integer age; public String getName() {return name; }public void setName(String name) {this.name = name; }public Integer getAge() {return age; }public void setAge(Integer age) {this.age = age; } }
The location is arbitrary (it is recommended to put it under src)
The configuration file name is arbitrary (it is recommended applicationContext.xml)
Import constraints:
Then edit applicationContext.xml
Enter editing Then click add to import xsi
After adding xsi, click add again to specify a new namespace
Then select the xsd you just imported
Click OK, return to the page just now, set the name of the namespace (you can directly copy the first half of the location Hint), leave prefix empty Then
Click OK and the following interface will be displayed, indicating that the import is successful.
Write applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans "><!-- 将User对象交给spring容器管理 --><bean name="user" class="cn.itcast.bean.User" ></bean></beans>
4. Code test
@Testpublic void fun1(){ //1 创建容器对象ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");//2 向容器"要"user对象User u = (User) ac.getBean("user");//3 打印user对象 System.out.println(u); }
3. Spring concept
1.2 di
2.applicationContext&BeanFactory
The container of the BeanFactory interface implementation class. The characteristic is that the object is created every time the object is obtained.
2.2 ApplicationContext
Load the configuration file from the class path: ClassPathXmlApplicationContext
2.3 Conclusion
4. Detailed explanation of spring configuration
<!-- 将User对象交给spring容器管理 --><!-- Bean元素:使用该元素描述需要spring容器管理的对象 class属性:被管理对象的完整类名. name属性:给被管理的对象起个名字.获得对象时根据该名称获得对象. 可以重复.可以使用特殊字符. id属性: 与name属性一模一样. 名称不可重复.不能使用特殊字符. 结论: 尽量使用name属性. --><bean name="user" class="cn.itcast.bean.User" ></bean>
singleton(默认值):单例对象.被标识为单例的对象在spring容器中只会存在一个实例
prototype:多例原型.被标识为多例的对象,每次再获得才会创建.每次创建都是新的对象.整合struts2时,ActionBean必须配置为多例的.
request:web环境下.对象与request生命周期一致.
session:web环境下,对象与session生命周期一致.
init-method:配置一个方法作为生命周期初始化方法.spring会在对象创建之后立即调用.
destory-method:配置一个方法作为生命周期的销毁方法.spring容器在关闭并销毁所有容器中的对象之前调用.
<bean name="user" class="cn.itcast.bean.User" init-method="init" destroy-method="destory" ></bean>
<!-- 创建方式1:空参构造创建(重点) --><bean name="user" class="cn.itcast.bean.User" init-method="init" destroy-method="destory" ></bean> <!-- 创建方式2:静态工厂创建 (了解) 调用UserFactory的createUser方法创建名为user2的对象.放入容器 --><bean name="user2" class="cn.itcast.b_create.UserFactory" factory-method="createUser" ></bean><!-- 创建方式3:实例工厂创建 (了解) 调用UserFactory对象的createUser2方法创建名为user3的对象.放入容器 --><bean name="user3" factory-bean="userFactory"factory-method="createUser2" ></bean><bean name="userFactory" class="cn.itcast.b_create.UserFactory" ></bean>
<!-- 导入其他spring配置文件 --><import resource="cn/itcast/b_create/applicationContext.xml"/>
<!-- set方式注入: --><bean name="user" class="cn.itcast.bean.User" ><!--值类型注入: 为User对象中名为name的属性注入tom作为值 --><property name="name" value="tom" ></property><property name="age" value="18" ></property><!-- 引用类型注入: 为car属性注入下方配置的car对象 --><property name="car" ref="car" ></property></bean><!-- 将car对象配置到容器中 --><bean name="car" class="cn.itcast.bean.Car" ><property name="name" value="兰博基尼" ></property><property name="color" value="黄色" ></property></bean>
<!-- 构造函数注入 --><bean name="user2" class="cn.itcast.bean.User" ><!-- name属性: 构造函数的参数名 --><!-- index属性: 构造函数的参数索引 --><!-- type属性: 构造函数的参数类型--><constructor-arg name="name" index="0" type="java.lang.Integer" value="999" ></constructor-arg><constructor-arg name="car" ref="car" index="1" ></constructor-arg></bean>
<!-- p名称空间注入, 走set方法 1.导入P名称空间 xmlns:p="http://www.springframework.org/schema/p" 2.使用p:属性完成注入 |-值类型: p:属性名="值" |-对象类型: p:属性名-ref="bean名称" --><bean name="user3" class="cn.itcast.bean.User" p:name="jack" p:age="20" p:car-ref="car" ></bean>
<!-- spel注入: spring Expression Language sping表达式语言 --><bean name="user4" class="cn.itcast.bean.User" ><property name="name" value="#{user.name}" ></property><property name="age" value="#{user3.age}" ></property><property name="car" ref="car" ></property></bean>
<!-- 如果数组中只准备注入一个值(对象),直接使用value|ref即可 <property name="arr" value="tom" ></property>--><!-- array注入,多个元素注入 --><property name="arr"><array><value>tom</value><value>jerry</value><ref bean="user4" /></array></property>
<!-- 如果List中只准备注入一个值(对象),直接使用value|ref即可 <property name="list" value="jack" ></property>--><property name="list" ><list><value>jack</value><value>rose</value><ref bean="user3" /></list></property>
<!-- map类型注入 --><property name="map" ><map><entry key="url" value="jdbc:mysql:///crm" ></entry><entry key="user" value-ref="user4" ></entry><entry key-ref="user3" value-ref="user2" ></entry></map> </property>
<!-- prperties 类型注入 --><property name="prop" ><props><prop key="driverClass">com.jdbc.mysql.Driver</prop><prop key="userName">root</prop><prop key="password">1234</prop></props></property>
再加1指的是:spring-web-4.2.4.RELEASE.jar(因为要用到web的监听)
<?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans "><!-- 配置Dao --><bean name="customerDao" class="cn.itheima.dao.impl.CustomerDaoImpl" ></bean><bean name="linkManDao" class="cn.itheima.dao.impl.LinkManDaoImpl" ></bean><bean name="userDao" class="cn.itheima.dao.impl.UserDaoImpl" ></bean><!-- 配置Service --><bean name="customerService" class="cn.itheima.service.impl.CustomerServiceImpl" ><property name="customerDao" ref="customerDao" ></property></bean><bean name="linkManService" class="cn.itheima.service.impl.LinkManServiceImpl" ><property name="cd" ref="customerDao" ></property><property name="lmd" ref="linkManDao" ></property></bean><bean name="userService" class="cn.itheima.service.impl.UserServiceImpl" ><property name="ud" ref="userDao" ></property></bean></beans>
<!-- 可以让spring容器随项目的启动而创建,随项目的关闭而销毁 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 指定加载spring配置文件的位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>
//获得spring容器=>从Application域获得即可 //1 获得servletContext对象ServletContext sc = ServletActionContext.getServletContext();//2.从Sc中获得ac容器WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(sc);//3.从容器中获得CustomerServiceUserService us = (UserService) ac.getBean("userService");
//创建容器对象ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");//获得cs(customerService对象)CustomerService cs = (CustomerService) ac.getBean("customerService");
The above is the detailed content of Detailed introduction to JAVAEE—spring. For more information, please follow other related articles on the PHP Chinese website!