org.springframework
spring-context
4.3.7.RELEASE
javax.inject
javax.inject
1
(1)声明Bean
<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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="duke" class="spring.test.bean.Juggler">
<constructor-arg value="15" />
<constructor-arg ref="sonnet29" />
bean>
<bean id="stage" class="spring.test.bean.Stage"
factory-method="getInstance" />
<bean id="duke" class="spring.test.bean.Ticket"
scope="prototype"/>
<bean id="kenny" class="spring.test.bean.Instrumentalist">
<property name="song" value="Jingle Bells" />
<property name="instrument" ref="saxophone" />
bean>
<bean id="saxophone" class="spring.test.bean.Saxophone" >
<property name="saxophone" value="#{SpEL}"
bean>
beans>
(1)自动装配Bean属性
<beans
-- 省略一大堆 -->
default-autowire="byType">
<bean id="duke" class="spring.test.bean.Juggler"
autowire="byType">
bean>
<bean id="duke2" class="spring.test.bean.Juggler"
primary="false">
bean>
<bean id="duke2" class="spring.test.bean.Juggler"
autowire-candidate="false">
bean>
beans>
(2)使用注解装配
<beans/>
<context:annotation-config>
<bean id="duke3" class="spring.test.bean.Juggler" >
<qualifier value="test" />
bean>
beans>
(3)自动检测Bean
<beans/>
<context:component-scan base-package="spring.test.bean">
<context:include-filter type="assignable"
expression="some.class" />
<context:exclude-filter type="assignable"
expression="some.class" />
context:component-scan>
beans>
(4)使用基于Java的配置
利用@Configuration注解来标识Java配置类
org.aspectj
aspectjweaver
1.8.10
org.aspectj
aspectjrt
1.8.10
aopalliance
aopalliance
1.0
在软件开发中,分布于应用中多处的功能被称为横切关注点。通常,这些横切关注点从概念上是与应用的业务逻辑相分离的。将这些横切关注点与业务逻辑相分离正是面向切面编程所要解决的。
(1)定义切点
execution(* spring.test.bean.Class.function(..)) && others
(2)在XML中声明切面
<beans -- 新添加 -->
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean
id="poeticDuke"
class="spring.test.bean.PoeticJuggler">
bean>
<bean
id="audience"
class="spring.test.bean.Audience">
bean>
<aop:config>
<aop:aspect ref="audience">
<aop:pointcut
id="performance"
expression="execution(* spring.test.bean.Performer.perform(..))" />
<aop:before
method="takeSeats"
pointcut="execution(* spring.test.bean.Performer.perform(..))" />
<aop:after-returning
method="applaud" -- applaud为audience的方法 -->
pointcut-ref="performance" />
aop:aspect>
aop:config>
beans>
// Audience 类中定义此方法,ProceedingJoinPoint作为方法的入参
public void watchPerformance(ProceedingJoinPoint joinPoint) {
try {
// do something
// 谨记必须调用proceed方法,否则本通知会阻塞被通知的方法,即perform()方法
joinpoint.proceed();
// do other things
}
}
<aop:config>
<aop:aspect ref="audience">
<aop:around
method="watchPerformance()" -- 先do something,然后调用perform(),再do other things -->
pointcut="execution(* spring.test.bean.Performer.perform(..))" />
aop:aspect>
aop:config>
<aop:aspect>
<aop:declare-parents
-- 匹配已有的bean -->
types-matching="spring.test.bean.Performer+"
implement-interface="spring.test.bean.NewInterface"
default-impl="spring.test.bean.InterfaceInstance"
/>
aop:aspect>
(3)基于注解的切面
@Aspect
public class Audience {
@Pointcut("execution(* spring.test.bean.Performer.perform(..))")
public void performance() {
}
@Before("performance()")
public void takeSeats() {
System.out.println("Take seat");
}
}
<aop:aspectj-autoproxy />
Spring切面仍然只是基于代理的,而且限于通知方法的调用。如果需要的功能超过了Spring所支持的方法代理,那么可以考虑使用AspectJ,Spring只有简单功能的aop。
(1)配置数据源
<bean id="dataSource class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://ip:3306/zhihu" />
<property name="username" value="root" />
<property name="password" value="" />
bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
bean>
<bean id="mysql" class="spring.test.db.Mysql">
<property name="jdbcTemplate" ref="jdbcTemplate" />
bean>
(2)使用JDBC DAO支持类
(3)Java持久化API
Spring提供的事务管理:
(1)事务管理器
example: JDBC事务
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
bean>
(2)声明式事务
(3)在xml中定义事务
<tx:advice id="txAdvice" transaction-manager="yourManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="*" propagation="SUPPORTS" read-only="true" />
tx:attributes>
tx:advice>
<aop:config>
<aop:advisor
pointcut="execution(* *..SpitterService.*(..))"
advice-ref="txAdvice" />
aop:config>
(4)在注解驱动中定义
<tx:annotation-driver transaction-manager="yourManager" />
@Transactional(propagation=Propagation.SUPPORTS, readOnly=true)
public class ServiceImpl implements Service {
...
@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
public void addSomething() {
}
}
(1)搭建Spring MVC
web.xml
<web-app>
<display-name>Archetype Created Web Applicationdisplay-name>
<servlet>
<servlet-name>weiboservlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
servlet-class>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>weiboservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
web-app>
weibo-servlet.xml
<mvc:resources
mapping="/resources/**"
location="/resources/" />
<mvc:annotation-driven />
<context:component-scan base-package="spring.test.mvc" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property
name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property
name="prefix"
value="/WEB-INF/views/" />
<property
name="suffix"
value=".jsp" />
bean>
(2)基本控制器
@Controller
public class HomeController {
public static final int DEFAULT_WEIBO_PER_PAGE = 25;
// 映射路径
@RequestMapping({"/", "/home"})
public String showHomePage(Map model) {
model.put("weibos", new WeiboHome());
return "home";
}
}
(3)控制器输入
@Controller
@RequestMapping("/weiboo") // 定义大的路径
public class Weiboo {
@RequestMapping(value="/boo", method=GET) // 路径为/weiboo/boo?name=lin
// 请求参数如果与下面的参数名一样,可省略@RequestParam("name")
// model是spring自己定义的map,会根据addAttribute的类,定义key
public String listWeiboo(@RequestParam("name") String name, Model model) {
model.addAttribute("name", name);
model.addAttribute("time", "2017");
return "weiboo";
}
}
weiboo.jsp
<%@ page isELIgnored="false" %>
<div>
<h1>This is weiboo of ${name}h1>
<h1>Time is ${time}h1>
div>