【java框架】0209笔记——Spring
2018-02-10 19:10
120 查看
一、spring环境搭建,IOC和DI
1.引入spring的jar包(jar包存入到百度网盘/源码/jar包/中),并且add path2.创建一个pojo类,用于演示控制反转(IOC),和依赖注入(DI)
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;
}
3.在src目录下配置applicationContext.xml,用于IOC和DI的演示,
通过关键字c即可获取Category对象,该对象获取的时候,即被注入了字符串"category 1“到name属性中
<?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="pojo.Category">
<property name="name" value="category 1" />
</bean>
</beans>
4.编写测试类,进行测试
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pojo.Category;
public class TestSpring {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml" });
Category c = (Category) context.getBean("c");
System.out.println(c.getName());
}
}
5.总结:IOC可以理解为由spring来创建对象,无须程序员再进行new;DI指的是有spring制动注入值;
二、注入对象
上一节中,注入了为category对象注入了一个name属性,这一节中,在product对象中,注入一个category对象。1.创建product对象
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;
}
}
2.使用ref,把上面创建的category c对象注入到product p对象之中
<bean name="p" class="com.how2java.pojo.Product">
<property name="name" value="product1" />
<property name="category" ref="c" />
</bean>
3.测试类方法:
Product p = (Product) context.getBean("p");
System.out.println(p.getName());
System.out.println(p.getCategory().getName());
三、通过注解方式进行IOC/DI操作
1.在bean前增加注解配置:<context:annotation-config/>,并且注释掉ref配置2.在Product.java的category属性前加上@Autowired注解,并且import org.springframework.beans.factory.annotation.Autowired;
3.直接在main方法中测试即可;
四、AOP面向切片编程
AOP 即 Aspect Oriented Program 面向切面编程。首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能。所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务。 所谓的周边功能,比如性能统计,日志,事务管理等等 。
周边功能在Spring的面向切面编程AOP思想里,即被定义为切面。在面向切面编程AOP的思想里面,核心业务功能和切面功能分别独立进行开发 。
然后把切面功能和核心业务功能 "编织" 在一起,这就叫AOP。
1.准备业务类ProductService
public class ProductService {
public void doSomeService() {
System.out.println("do some service");
}
2.在main方法中调用此方法
五、使用注解方式进行测试
1.单元测试使用junit-4.12.jar,hamcrest-all-1.3.jar 两个jar包;2.在applicationContext.xml配置好bean;
3.重写测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestSpring {
@Autowired
Category category;
@Test
public void test(){
System.out.println(category.getName());
}
}
相关文章推荐
- Java框架spring 学习笔记(八):注入对象类型属性
- Java框架学习笔记——初识Spring之简述
- Java框架spring 学习笔记(十三):log4j介绍
- 我的java框架学习笔记spring入门及…
- Java框架spring 学习笔记(十八):事务管理(xml配置文件管理)
- Java框架spring 学习笔记(十四):注解aop操作
- Java框架spring 学习笔记(九):Spring的bean管理(@Required、@Component、@Autowired、@Resource注解)
- Java框架spring 学习笔记(三):Bean 的生命周期
- JAVA框架学习笔记 | Spring学习
- 1. 笔记JAVA框架学习——Spring入门环境搭建及helloworld
- Java框架学习笔记——Spring开发环境搭建
- Java框架spring 学习笔记(四):BeanPostProcessor接口
- Java框架spring 学习笔记(七):Spring相关概念
- Java框架spring 学习笔记(十五):操作MySQL数据库
- SSM框架笔记09:初探Spring——采用Java配置方式
- Java框架spring 学习笔记(十二):aop实例操作
- Java框架spring 学习笔记(二):Bean的作用域
- Java框架spring 学习笔记(一):SpringBean、ApplicationContext 容器、BeanFactory容器
- java开源框架应用技巧之spring配置文件中如果有多个.hbm.xml文件的话,无论是项目开发过程中还是维护过程中修改起来都会很麻烦切容易出错
- java框架简介1--spring+hibernate简介 2 用例驱动编程