Spring_01笔记
Spring是什么?
Spring 是分层的 Java SE/EE 应用 full-stack 轻量级开源框架,以 IoC(Inverse Of Control:
反转控制)和 AOP(Aspect Oriented Programming:面向切面编程)为内核,提供了展现层 Spring
MVC 和持久层 Spring JDBC 以及业务层事务管理等众多的企业级应用技术,还能整合开源世界众多
著名的第三方框架和类库,逐渐成为使用最多的 Java EE 企业应用开源框架。
1.IOC的概念和作用
1.程序的耦合
耦合: 程序的耦合是程序模块之间的依赖关系,包括控制关系,调用关系,数据传递关系。模块间的联系越多,耦合性越强。内聚:内聚是一个模块内各个元素彼此结合的紧密程度。我们需要减少耦合,提高内聚。
例如:
public class AccountServiceImpl implements AccountService{
private AccountDao dao = new AccountDao();
}
业务层调用持久层时,如果此时没有持久层的实现类,编辑就不能通过,这种编译期依赖关系,我们应该优化他。
2.解耦的思路
(1)工厂模式解耦
在实际开发中我们可以把三层的对象都使用配置文件配置起来,当启动服务器应用加载的时候, 让一个类中的方法通过读取配置文件,把这些对象创建出来并存起来。在接下来的使用的时候,直接拿过来用就好了。那么,这个读取配置文件, 创建和获取三层对象的类就是工厂 .。
(2)控制反转(IOC)
工厂负责给我们从容器中获取指定对象的类,这时我们获取对象的方式发生了改变,原来是我们new出来的,是主动的,现在是工厂为我们查找或创建对象,是被动的。
3.使用Sping的IOC解决程序耦合
(1)准备spring的jar包 (spring-framework)
(2)创建配置文件,导入约束
<?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">
</beans>
(3)让spring管理资源,在配置文件中配置service和dao
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
</bean>
<bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl"></bean>
(4)获取容器执行
public static void main(String[] args ){
//使用ApplicationContext接口,获取Spring容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//根据beanid 获取对象
IAccountService aService = (IAccountService) ac.getBean("accountService") ;
//IAccountService aService = ac.getBean("accountService",IAccountService.class) ;
//Dao层一样
}
BeanFactory 和 ApplicationContext 的区别 :BeanFactory 才是 Spring 容器中的顶层接口,
ApplicationContext 是它的子接口。
BeanFactory 和 ApplicationContext 的区别:
创建对象的时间点不一样。
ApplicationContext:只要一读取配置文件,默认情况下就会创建对象。
BeanFactory:什么使用什么时候创建对象
4.IOC中bean的标签和管理对象细节
1.bean标签
属性:
id:给对象在容器中提供一个唯一标识。用于获取对象。
class:指定类的全限定类名。用于创建反射对象。默认情况下调用无参构造参数。
scope:指定对象的作用范围 * singten:默认值,单例 *prototype:多例, *request:web项目中,spring创建一个对象存入request域中, *session:同理,存入session域, *global session:web项目中,应用在 Portlet 环境.如果没有 Portlet 环境那么
globalSession相当于session。
2.实例化bean的三种方式
第一种:使用默认无参构造函数
它会根据默认无参构造函数来创建类对象。如果 bean 中没有默认无参构造函数,将会创建失败。
第二中:使用静态工厂创建对象
public class StaticFactory {
public static IAccountService createAccountService(){
return new AccountServiceImpl();
}
}
使用 StaticFactory 类中的静态方法 createAccountService 创建对象,并存入 spring 容器
id 属性:指定 bean 的 id,用于从容器中获取
class 属性:指定静态工厂的全限定类名
factory-method 属性:指定生产对象的静态方法
<bean id="accountService"
class="com.itheima.factory.StaticFactory"
factory-method="createAccountService"></bean>
第三种:使用实例工厂的方法创建对象
public class InstanceFactory {
public IAccountService createAccountService(){
return new AccountServiceImpl();
}
}
先把工厂的创建交给 spring 来管理。
然后在使用工厂的 bean 来调用里面的方法
factory-bean 属性:用于指定实例工厂 bean 的 id。
factory-method 属性:用于指定实例工厂中创建对象的方法。
<bean id="instancFactory" class="com.itheima.factory.InstanceFactory"></bean>
<bean id="accountService"
factory-bean="instancFactory"
factory-method="createAccountService"></bean>
3.spring的依赖注入
1.构造函数注入
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
<constructor-arg name="name" value="张三"></constructor-arg>
<constructor-arg name="age" value="18"></constructor-arg>
<constructor-arg name="birthday" ref="now"></constructor-arg>
</bean>
<bean id="now" class="java.util.Date"></bean>
2.set方法注入(常用)
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
<property name="name" value="test"></property>
<property name="age" value="21"></property>
<property name="birthday" ref="now"></property>
</bean>
<bean id="now" class="java.util.Date"></bean>
3.P名称空间注入
配置加一行 xmlns:p=“http://www.springframework.org/schema/p” 就可以用,本质还是set
4.注入注解属性
Set,List,Array结构
<property name="myStrs">
<set>
<value>AAA</value>
<value>BBB</value>
<value>CCC</value>
</set>
</property>
Map,properties,结构
<property name="myMap">
<props>
<prop key="testA">aaa</prop>
<prop key="testB">bbb</prop>
</props>
</property>
只要结构相同,标签可以互换
2.通过注解配置IOC
1.使用@Component注解配置管理的资源
@Controller:一般用于表现层的注解
@Service:一般用于业务层的注解
@Reposity:一般用于持久层(不太清楚为什么。。先这样记了)
2.用于注入数据
@Autowired:自动注入
@Qualifier:和上面的一起使用,在多个变量时根据bean的id注入;在给方法参数注入时可以单独使用
@Resource:直接按照bean的id注入
@Value:注入基本数据类型和String
@Scpoe:指定作用范围
…
3.不使用xml时,配置类
@Configuration :指定当前的类为配置类,当创建容器时会从该类上加载注解。
@ConponentScan :相当于xml中的扫描
@Bean : 把当前方法的返回值作为bean对象存入ioc容器中
细节:用注解配置方法时,如果方法有参数,spring框架会去容器中寻找有无可用的bean对象
此时默认是单例的,需要@Scpoe(“prototype”)
@PropertySource:用于加载.properties中的配置,例如数据库。