- 浏览: 170262 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (193)
- Axis2 (10)
- Andriod (2)
- Java (22)
- Eclipse (2)
- 程序人生 (3)
- Windows (1)
- Sql Server 2005/2008 (7)
- 健身 (2)
- Log4j (1)
- Ant (1)
- Fatjar (2)
- 国际化 (1)
- Linux (3)
- JDBC (1)
- Oracle (2)
- 各种报错 (4)
- SWT (5)
- Tomcat (2)
- 车辆管理 (1)
- SVN (2)
- Spring (5)
- 域名服务器 (0)
- HaoWaYa (1)
- FTP (1)
- 集散中心 (1)
- 专业知识 (1)
- 面试准备 (19)
- 设计模式 (22)
- Junit (1)
- 软件下载 (3)
- 深入理解Java虚拟机 (3)
- 数据结构 (4)
- 雅思 托福 (0)
- UML (1)
- Maven (1)
- CV (1)
- ServiceMix (1)
- 电子书 (5)
- Struts1/2 (4)
- DOM W3C DHTML (3)
- Jawr (1)
- LoadRunner (1)
- Java反编译 (0)
- 英语学习 (0)
- 技术书籍 (1)
- Cygwin (0)
- ibatis (1)
- 数据库 (1)
- jQuery (0)
- s (2)
- 源代码项目 (5)
- JSRs (0)
- JCP (0)
- XML (2)
- Dojo (3)
- Effective Java (1)
- 一站到底 (3)
- JavaScript (6)
- DB2 (1)
- 刷机 (1)
- 字符 (1)
- Dynamic Web Project (1)
- 股市日记 (1)
- 代码片段 (0)
- CSS (1)
- PDF (0)
- 英语口语 (1)
- 乒乓球 (1)
- 体检 (0)
- 送花 (0)
- 面试准备-再战江湖 (5)
- ddq (0)
- sss (0)
- ssssss (0)
- 2020面试 (0)
最新评论
-
samsongbest:
Copperfield 写道你的目标很远大,佩服~惭愧,都忘了 ...
人生目标 -
Copperfield:
你的目标很远大,佩服~
人生目标
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>
最后保留行
- Spring2.5_library.zip (3.2 MB)
- 描述: Spring2.5 lib包
- 下载次数: 3
- spring_html_single.zip (2.2 MB)
- 下载次数: 0
- spring_exercise_code.zip (99.5 KB)
- 下载次数: 1
- aopalliance.jar (4.4 KB)
- 下载次数: 1
- aspectjrt.jar (112.2 KB)
- 下载次数: 0
- aspectjweaver.jar (1.5 MB)
- 下载次数: 0
- cglib-nodep-2.1_3.jar (316.6 KB)
- 下载次数: 0
- spring_exercise_code.zip (125.9 KB)
- 下载次数: 2
相关推荐
This can save you a great deal of work over creating a project from scratch! What you’ll learn * How to use the IoC container and the Spring application context to best effect. * Spring’s AOP ...
This can save you a great deal of work over creating a project from scratch. Using a problem-solution approach, Spring Boot 2 Recipes quickly introduces you to Pivotal's Spring Boot 2 micro-...
Build an automated currency trading bot from scratch with java. In this book, you will learn about the nitty-gritty of automated trading and have a closer look at Java, the Spring Framework, event-...
This book will help you understand how to implement microservice-based systems from scratch. You’ll start off by understanding the core concepts and framework, before focusing on the high-level ...
This book will help you understand how to implement microservice-based systems from scratch. You’ll start off by understanding the core concepts and framework, before focusing on the high-level ...
Full Stack AngularJS for Java Developers Build a Full-Featured Web Application from Scratch Using AngularJS with Spring RESTful
Unlike many Flex books that presume readers want to develop applications from scratch, Flex on Java is for developers in the real world-where Flex is one more technology being added to existing ...
Understand and create multiple types of reports and dashboards from scratch Assign permissions related to reports and dashboards Get to grips with the Salesforce data architecture and its relation to ...