发表于: 2017-05-09 22:08:12
2 1440
今日计划
spring学习,spring和mybaits的整合
今日完成
昨天日报偷懒了,今天补上。
spring是一个基于ioc和aop的结构j2ee系统的框架
IOC反转控制是spring的基础,inversion of control
简单的说就是创建对象由以前的程序员自己new构造方法来调用,变成了交由spring创建对象
DI依赖注入Dependency Inject,简单的说就是拿到对象的属性,已经被注入好相关值了,直接使用即可。
spring注入对象
product类中对category对象的setter getter
pojo
package com.how2java.pojo;
public class Product {
private int id;
private String name;
private Category category;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
}
在创建Product的时候注入一个Category对象
这里要使用ref来注入另一个对象
applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean name="c" class="com.how2java.pojo.Category">
<property name="name" value="category 1" />
</bean>
<bean name="p" class="com.how2java.pojo.Product">
<property name="name" value="product1" />
<property name="category" ref="c" />
</bean>
</beans>
TestSpring
package com.how2java.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.how2java.pojo.Product;
public class TestSpring {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml" });
Product p = (Product) context.getBean("p");
System.out.println(p.getName());
System.out.println(p.getCategory().getName());
}
}
通过Spring拿到的Product对象已经被注入了Category对象
注解方式IOC/DI
修改applicationContext.xml
添加<context.annotion-config>告诉Spring要用注解方式进行匹配值
注入对象的注释掉,这个行为在后面用注解来完成
@Autowired
在Product.java的category属性前加上@Autowired注解
运行debug
取得相同结果
除了在属性前加上@Autowired这种方式之外,也可以在setCategory方法前加上@Autowired达到相同效果
除了@Autowired之外,@Resource也是常用手段
以上是对注入对象行为的注解,bean对象本身,也可以通过注解进行
其作用是告诉Spring,bean都放在com.how2java.pojo这个包下
<context:component-scan base-package="com.how2java.pojo"/>
为Product类加上@Component注解,即表明此类是bean
package com.how2java.pojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component("p")
public class Product {
private int id;
private String name="pdoduct 1";
@Autowired
private Category category;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
}
为Category类加上@Component注解,表明此类是bean
package com.how2java.pojo;
import org.springframework.stereotype.Component;
@Component("c")
public class Category {
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private int id;
private String name;
}
运行debug,结果一样
AOP
AOP 即 Aspect Oriental Program面向切面编程
首先,在面向切面编程的思想里面,把功能分为核心业务功能和周边功能
所谓核心业务,比如登陆,增加数据,删除数据都叫核心业务
所谓周边功能,比如性能统计,日志,事务管理等
周边功能在Spring的变相切面编程AOP思想里,即被定义为切面
在面向切面编程AOP的思想里面,核心业务功能和切面功能分别独立进行开发
然后把切面功能和核心业务功能编织在一起,这就叫AOP
业务类ProductService
package com.how2java.service;
public class ProductService {
public void doSomeService(){
System.out.println("doSomeService");
}
}
TestSpring
在引入切面之前,调用该业务类
package com.how2java.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.how2java.service.ProductService;
public class TestSpring {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
ProductService s = (ProductService) context.getBean("s");
s.doSomeService();
}
}
准备日志切面LoggerAsppect
在日志切面的功能是 在调用核心功能之前和之后分别打印日志
package com.how2java.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
public class LoggerAspect {
public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("start log:" + joinPoint.getSignature().getName());
Object object = joinPoint.proceed();
System.out.println("end log:" + joinPoint.getSignature().getName());
return object;
}
}
applicationContext
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean name="c" class="com.how2java.pojo.Category">
<property name="name" value="yyy" />
</bean>
<bean name="p" class="com.how2java.pojo.Product">
<property name="name" value="product1" />
<property name="category" ref="c" />
</bean>
<bean name="s" class="com.how2java.service.ProductService">
</bean> //声明业务对象
<bean id="loggerAspect" class="com.how2java.aspect.LoggerAspect"/>
//声明日志切面
<aop:config>
<aop:pointcut id="loggerCutpoint"
expression=
"execution(* com.how2java.service.ProductService.*(..)) "/>
//制定核心业务功能
<aop:aspect id="logAspect" ref="loggerAspect">
<aop:around pointcut-ref="loggerCutpoint" method="log"/>
</aop:aspect>//指定辅助功能
</aop:config> //把业务对象和辅助功能编制在一起
</beans>
debug结果
业务运行之前和之后会分别打印日志
注解配置业务类
使用@CComponent("s")注解ProductService类
package com.how2java.service;
import org.springframework.stereotype.Component;
@Component("s")
public class ProductService {
public void doSomeService(){
System.out.println("doSomeService");
}
}
注解配置切面
@Aspect 注解表示这是一个切面
@Component 表示这是一个bean,由Spring进行管理
@Around(value = "execution(* com.how2java.service.ProductService.*(..))") 表示对com.how2java.service.ProductService 这个类中的所有方法进行切面操作
package com.how2java.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LoggerAspect {
@Around(value = "execution(* com.how2java.service.ProductService.*(..))")
public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("start log:" + joinPoint.getSignature().getName());
Object object = joinPoint.proceed();
System.out.println("end log:" + joinPoint.getSignature().getName());
return object;
}
}
applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.how2java.aspect"/>//扫描包,定位切面类
<context:component-scan base-package="com.how2java.service"/>//扫描包,定位业务类
<aop:aspectj-autoproxy/> //找到被注解了的切面类,进行切面配置
</beans>
测试结果
注解方式整合spring和junit
下载junit-4.12.jar和hamcrest-all-1.3.jar
修改TestSpring
package com.how2java.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.how2java.pojo.Category;
@RunWith(SpringJUnit4ClassRunner.class)//表示这是一个spring的测试类
@ContextConfiguration("classpath:applicationContext.xml")//定位spring配置文件
public class TestSpring {
@Autowired
Category c; //给这个测试类装配Category对象
@Test
public void test(){
System.out.println(c.getName()); //测试逻辑打印c对象名称
}
}
配置mybaits
下载jar包
mybaits3.4.2并导入
创建实体类,映射表
创建配置文件mybaits-config.xml
提供链接数据库用的驱动,数据库名称,编码方式,账号密码
mybaits和spring整合
创立mapper层
xml配置文件和mapper类放在同一个包下,并且namespacce必须写mapper文件名
编写applicationContext.xml文件
用spring注解方式测试
遇到问题
使用范例进行整合spring mybatis测试报错,应该是log4j配置出了问题
收获
注解法spring整合junit,mybatis和spring整合
明日计划
学习mybatis和log4j
评论