在这里我想说一句spring很强大
-
为每一个层都提供解决方案
- web层:struts,spring-MVC
- service层:spring
- dao层:hibernate,mybatis , jdbcTemplate SpringData
-
Spring的体系结构
3.Spring的优点
- 方便解耦,简化开发
- AOP编程的支持
- 声明式事务的支持
- 方便程序的测试
- 方便集成各种优秀框架
- 降低Java EE API的使用难度
4.Spring的核心
1.Spring的核心是控制反转(IoC)和面向切面(AOP)
AOP的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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--设置切面类的自动代理 -->
<aop:aspectj-autoproxy />
<!-- 开启扫描模式,扫描指定包,这样就不需要开启注解模式了-->
<context:component-scan base-package="com.qf"/>
<bean id="myAspect" class="com.qf.aspect.MyAspect"></bean>
<aop:config>
<!-- 配置切面 -->
<aop:aspect ref="myAspect">
<aop:pointcut expression="execution(* com.qf.service.impl.*.*(..))" id="myPoint"/>
<!-- 切面类中的方法名配置,并引入切点-->
<aop:before method="before" pointcut-ref="myPoint"/>
<aop:after method="after" pointcut-ref="myPoint"/>
</aop:aspect>
</aop:config>
</beans>
2.IOC:控制反转,将对象创建的权利转移到第三方的容器来管理
创建spring主配置文件管理bean
5.SpringDI(依赖注入)
1.手动装配
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">
<!--
依赖注入:
UserService中包含UserDao的属性
name:代表属性名
ref: 要去引用的bean对象,这样在UserService中的属性userDao就有值了
-->
<!-- xml的自动装配:
autowire="constructor": 以构造器的方式自动装配
autowire="byName" : 以setter的方式自动装配,id匹配set名
autowire="byType" : 匹配的是类型
如果有两个bean,则不能使用byType,系统不知道找哪个类型
-->
<bean id="userService" class="com.qf.service.impl.UserServiceImpl" autowire="byName">
<!-- <property name="userDao" ref="userDao1"></property> -->
<!-- <constructor-arg name="userDao" ref="userDao1"></constructor-arg> -->
</bean>
<bean id="userDao" class="com.qf.dao.impl.UserDaoImpl"></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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 开启扫描模式,扫描指定包,这样就不需要开启注解模式了-->
<context:component-scan base-package="com.qf"/>
</beans>
@Autowired,自动装配依赖,默认是按照类型来装配
@Resource(name="userDao"),自动按照名字来装配
@Component
@Scope("prototype")
@Repository:dao层
@Service:service层
@Controller:web层