1. IOC - Inversion of Control 控制反转
IOC的基本概念是:不创建对象,但是描述创建它们的方式。在代码中不直接与对象和服务连接,但在配置文件中描述哪一个组件需要哪一项服务。容器负责将这些联系在一起。
2. AOP - Aspect Oriented Programming
意为:面向切面编程(也叫面向方面),可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术。AOP实际是GoF设计模式的延续,设计模式孜孜不倦追求的是调用者和被调用者之间的解耦,AOP可以说也是这种目标的一种实现。
对业务逻辑的各个部分进行隔离, 从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
3. 加载
3.1 FileSystemXmlApplicationContext
ApplicationContext context = new FileSystemXmlApplicationContext(
new String[] { "src/services.xml" });
3.2 ClassPathXmlApplicationContext
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"services.xml"});
4. 配置文件 service里调用dao
<?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-2.5.xsd">
<bean id="userService" class="com.sam.service.UserService" lazy-init="true">
<property name="userDao" ref="userDao" />
</bean>
<bean id="userDao" class="com.sam.dao.UserDaoImpl2">
<!-- collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions go here -->
</beans>
5. Annotation @命令的自动提示

6. Spring anotation的配置
配置文件(service中的dao不再需要setter,getter方法):
<?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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config/>
<bean id="userService" class="com.sam.service.UserService"></bean>
<import resource="daos.xml" />
</beans>
类:
package com.sam.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.sam.dao.UserDao;
public class UserService {
@Autowired
public UserDao userDao;
public void save() {
System.out.println("UserService saved!");
userDao.save();
}
}
7. Classpath scanning for managed components 零配置:
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="com.sam" />
</beans>
DAO:
package com.sam.dao;
import org.springframework.stereotype.Repository;
@Repository
public class UserDao {
public void save() {
System.out.println("UserDao saved!");
}
}
Service:
package com.sam.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sam.dao.UserDao;
@Service
public class UserService {
@Autowired
public UserDao userDao;
public void save() {
System.out.println("UserService saved!");
userDao.save();
}
}
8. PropertyEditors
好像蛮重要的,有空看一下
9. Chapter 6. Aspect Oriented Programming with Spring
AOP的注解的配置:
<?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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="com.sam" />
<aop:aspectj-autoproxy/>
</beans>
Java类的配置:
package com.sam.aop;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LogInterceptor{
@Before("execution(public void com.sam.dao.UserDaoImpl2.userSaved())")
public void beforeMethod() {
System.out.println("m started.");
}
}
第一次运行报错,缺少类,加入如下3个包解决问题:
Spring_lib_with_dependency\aspectj\aspectjrt.jar
Spring_lib_with_dependency\aspectj\aspectjweaver.jar
Spring_lib_with_dependency\aopalliance\aopalliance.jar
类继承了接口,可以自动由jdk的InvocationHandler来实现代理。
类没有接口,需要加入cglib来实现。
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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="com.sam" />
<bean id="logInterceptor" class="com.sam.aop.LogInterceptor" />
<aop:config>
<aop:pointcut expression="execution(public * com.sam.service..*.userSaved(..))"
id="servicePointcut" />
<aop:aspect id="logAspect" ref="logInterceptor">
<aop:before method="beforeMethod" pointcut-ref="servicePointcut" />
</aop:aspect>
</aop:config>
</beans>
10. 配置datasource
方式一: 直接写连接信息,用户名密码
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="url" value="jdbc:sqlserver://localhost:1433;DatabaseName=db"/>
<property name="username" value="sam"/>
<property name="password" value="sam"/>
</bean>
方式二: 配JNDI
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<!-- websphere -->
<property name="jndiName" value="java:comp/env/jdbc/samproject" />
<!--tomcat -->
<property name="jndiName" value="jdbc/samproject" />
</bean>
在tomcat的service.xml配置
<Context docBase="samproject" path="/samproject" reloadable="true" debug="5" crossContext="true"
source="org.eclipse.jst.j2ee.server:samproject">
<Resource name="jdbc/samproject"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="jdbc:sqlserver://localhost:1433;DatabaseName=db"
username="sam"
password="sam"
maxActive="100"
maxIdle="30"
maxWait="10000" />
</Context>
</Host>
11. 事务管理
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> <tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> <tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception" /> <tx:method name="*" read-only="false"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="serviceMethod" expression="execution(* com.sam.service..*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" /> </aop:config>
最后保留行
本文介绍了Spring框架的核心概念如IOC、AOP及其配置方法,包括XML和注解配置方式,详细阐述了如何通过Spring管理组件和服务,进行事务管理及数据源配置。

2365

被折叠的 条评论
为什么被折叠?



