一:Spring概念
二、Spring的搭建
1、jar包
(1) spring-core.jar
这个jar文件包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工具类。
(2) spring-beans.jar
这个jar文件是所有应用都要用到的,它包含访问配置文件、创建和管理bean以及进行Inversion of Control / Dependency Injection(IoC/DI)操作相关的所有类。如果应用只需基本的IoC/DI支持,引入spring-core.jar及spring-beans.jar文件就可以了。
(3) spring-context.jar
这个jar文件为Spring核心提供了大量扩展。可以找到使用Spring ApplicationContext特性时所需的全部类,JDNI所需的全部类.
(4)spring-expression-4.1.6.RELEASE.jar
Expression Language模块提供了一个强大的表达式语言用于在运行时查询和操纵对象。
2、配置文件 applicationContext.xml
放在src下
<?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"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
</beans>
三、xml的Bean对象的三种创建方式
1:构造器 id属性:标识符 class属性:bean类型
<bean id="user" class="com.tlxy.entity.User"></bean>
2:静态工厂
语法格式:bean id=“标识符” class=“包名.类名” factory-method=“静态方法”
<bean id="staticFactory" class="com.tlxy.entity.UserFactory" factory-method="createUser"></bean>
3:动态(实例)工厂
语法格式:语法格式:
bean id=“标识符” class=“包名.类名”
bean id=“标识符” factory-bean=“标识符” factory-method=“动态方法”
<bean id="user2" class="com.tlxy.entity.UserFactory"></bean>
<bean id="dFactory" factory-bean="user2" factory-method="dcreateUser"></bean>
使用spring容器创建对象
package com.tlxy.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.tlxy.entity.User;
public class applicationContextTest1 {
public static void main(String[] args) {
//1.创建容器
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.从ApplicationContext获取Bean对象
/**
* 语法格式:
* 类型 变量名 = 容器对象.getBean("标识符",组件类型)
* */
//User user = ac.getBean("user", User.class);构造器
//User user = ac.getBean("staticFactory", User.class);静态工厂
User user = ac.getBean("dFactory", User.class);
System.out.println(user);
}
}
四、Bean的单例和多例
在Spring中,bean的Scope常被定义的两种模式:prototype(多例)和singleton(单例)。
singleton(单例):
只有一个共享的实例存在,所有对这个bean的请求都会返回这个唯一的实例。
prototype(多例):
对这个bean的每次请求都会创建一个新的bean实例,类似于new。
<!--
default-init-method="方法名"
default-destroy-method="方法名"
作用在beans节点上的方法,只要bean对象中包含了相同的方法都会默认执行
-->
<!--
在默认情况下,每一个bean都是单例的
在单列情况下,随着容器的创建而创建
scope属性:作用域
scope = singleton 单列(默认项)
scope = prototype 多列
request gloabl session 不常用
<bean id="stu" class="com.tlxy.entity.Student" scope="prototype"></bean>
-->
<!--
延迟加载 lazy-init 延迟加载 当设置成true是,当你调用getBean()时候才会创建对象,不管是单例还是多例。
init-method=""对象初始化时执行
destroy-method=""对象销毁时执行的方法
-->
<bean id="stu" class="com.tlxy.entity.Student" init-method="init" destroy-method="destroy" lazy-init="true"></bean>
</beans>
五、Bean的参数注入
1:setter注入
(1)、字符串注入
<bean id="u" class="com.tlxy.entity.User">
<!-- 字符串注入 -->
<property name="id" value="1"></property>
<property name="username" value="admin"></property>
<property name="password" value="123"></property>
</bean>
(2)、对象注入
<bean id="u" class="com.tlxy.entity.User">
<!-- 字符串注入 -->
<property name="id" value="1"></property>
<property name="username" value="admin"></property>
<property name="password" value="123"></property>
</bean>
<bean id="stu" class="com.tlxy.entity.Student">
<property name="sid" value="1"></property>
<property name="sname" value="feifei"></property>
<!-- ref属性:对象注入 -->
<property name="user" ref="u"></property>
</bean>
(3)、list集合注入
<bean id="mb" class="com.tlxy.entity.MessegeBean">
<property name="slist">
<!-- 注入list集合 -->
<list>
<value>wdnmd</value>
<value>裂开</value>
<value>难受</value>
<value>马飞</value>
</list>
</property>
(4)、set集合注入
<property name="stuSet">
<!-- 注入set集合 -->
<set>
<!-- 注入对象 -->
<ref bean="s1"/>
<ref bean="s2"/>
</set>
</property>
(5)、map集合注入
<property name="map">
<map>
<entry key="婴" value="哼"></entry>
<entry key="嘤嘤" value="哼哼"></entry>
<entry key="嘤嘤嘤" value="哼哼哼"></entry>
</map>
</property>
(6)propos集合注入
property name="pros">
<props>
<prop key="username">root</prop>
<prop key="password">1234</prop>
<prop key="url">jdbc://localhost:3306/tlxy?severTimeZone=UTC</prop>
<prop key="driverClass">com.mysql.cj.jdbc.Driver</prop>
</props>
</property>
2:构造器注入
字符串和对象
<!--
2:构造器注入
<constructor-arg index="0代表第一个参数" value="参数值"></constructor-arg>
<constructor-arg index="0代表第一个参数" ref="对象id"></constructor-arg>
-->
<bean id="stu2" class="com.tlxy.entity.Student">
<constructor-arg index="0" value="2"></constructor-arg>
<constructor-arg index="1" value="兵兵"></constructor-arg>
<constructor-arg index="2" ref="u"></constructor-arg>
六、Spring常用注解。
@Service用于标注业务层组件、
@Controller用于标注控制层组件(如struts中的action)
@Repository用于标注数据访问组件,即DAO组件。
@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
@Autowired 默认按类型装配,如果我们想使用按名称装配,可以结合@Qualifier注解一起使用。如下:
@Autowired @Qualifier(“personDaoBean”) 存在多个实例配合使用
@Resource默认按名称装配,当找不到与名称匹配的bean才会按类型装配。
使用注解
applicationContext.xml,扫描项目中的注解。
<!-- 开启组件扫描 -->
<context:component-scan base-package="com.tlxy"></context:component-scan>
Java
先定义一个叫做UserDao的接口。
package com.tlxy.dao;
public interface UserDao {
public void delete();
}
创建一个类叫做HIbernate的类实现UserDao接口,假设这里实现的是对数据库的操作,使用Repository注解把HIbernate放入到spring容器中,注解是告诉Spring让Spring创建一个名字叫"hdao"的HIbernate实例
package com.tlxy.dao;
import org.springframework.stereotype.Repository;
@Repository("hdao")//由spring管理
public class HIbernate implements UserDao {
@Override
public void delete() {
System.out.println("通过hibernate实现用户删除");
}
}
JDBCDao也是实现UserDao接口然后放入到Spring中。
package com.tlxy.dao;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
@Repository("jdao")
public class JDBCDao implements UserDao{
public void delete() {
System.out.println("通过jdbc实现用户删除");
}
}
把UserController作为控制层使用@Controller注解放入到spring容器,然后spring根据标签@Autowired、@Qualifier(“hdao”)知道要使用JDBCDao实例创建一个对象注入到UserDao类型的dao中。
package com.tlxy.Cnotroller;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import com.tlxy.dao.UserDao;
@Controller("ucontroller")//被spring管理
public class UserController {
// @Resource(name="jdao")
@Autowired
@Qualifier("hdao")
private UserDao dao;
public void delete() {
dao.delete();
}
}
最后测试时创建容器使用getBean()方法通过标识符和泛型类创建controller,controller里实现了HIbernate实例,所以调用的delete方法也是使用HIbernate实例的delete()方法。
package com.tlxy.test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.tlxy.Cnotroller.UserController;
import com.tlxy.dao.JDBCDao;
public class ApplicationConText2 {
public static void main(String[] args) {
AbstractApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserController controller = ac.getBean("ucontroller",UserController.class);//解耦
controller.delete();
}
}