目录
一.spring介绍
二. IOC(依赖注入和控制反转)
使用注解进行配置
三.AOP(面向切面编程)
四.使用注解实现声明式事物
一.spring介绍
(1)Spring 2003年出生 , IOC 和 AOP两大基础 IOC 控制反转 (DI 依赖注入) AOP 面向切面编程;
(2)现在发展为:
- 二. IOC(依赖注入和控制反转)
1.在pom.xml添加依赖
commons-logging
commons-logging
1.2
org.springframework
spring-context
5.3.16
org.springframework
spring-expression
5.3.16
org.springframework
spring-beans
5.3.16
org.springframework
spring-core
5.3.16
org.springframework
spring-web
5.3.16
org.springframework
spring-aop
5.3.16
2.创建实体类
public class User1 {
private int id;
private String name;
private int age;
public User1() {
}
public User1(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "id:"+this.id+" name:"+this.name+" age:"+this.age;
}
}
3.在spring.xml中配置bean
4. 测试
public class test {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
User1 user1= (User1) context.getBean("user1");
System.out.print(user1);
}
}
执行结果
使用注解进行配置1.创建实体类
@Component("user")
@PropertySource(value = "classpath:c.properties",encoding ="utf-8")
public class User2 implements Serializable {
@Value("${id}")
private int id;
@Value("${name}")
private String name;
@Value("${age}")
private int age;
public User2() {
}
public User2(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "id:"+this.id+"名称:"+this.name+" 年龄:"+this.age;
}
}
2.在spring.xml中配置扫描包
3.创建c.properties文件并在其中配置value
4.测试
三.AOP(面向切面编程)1.导入依赖
org.aspectj
aspectjrt
1.8.1
aopalliance
aopalliance
1.0
org.aspectj
aspectjweaver
1.8.1
org.springframework
spring-aspects
5.3.19
2.创建通知类
public class aspect {
//前置通知
public void before(JoinPoint joinPoint){
System.out.print("目标方法:"+joinPoint.getSignature().getName()+"-这是前置通知");
}
//后置通知
public void after(JoinPoint joinPoint){
System.out.print("目标方法:"+joinPoint.getSignature().getName()+"*这是后置通知");
}
//环绕通知
public void around(ProceedingJoinPoint pj){
System.out.print("目标方法:"+pj.getSignature().getName()+"#这是环绕置通知");
}
//异常通知
public void exception(){
System.out.print("这是异常通知");
}
}
3.创建方法
public class Log {
public void insert() {
System.out.print("插入数据成功");
}
public void delete() {
System.out.print("已被删除");
}
}
4.配置spring-aop.xml
5.测试
使用注解实现aop
1.创建方法
public class Log {
public void insert() {
System.out.print("插入数据成功");
}
public void delete() {
System.out.print("已被删除");
}
}
2.创建方法
@Component
@Aspect
public class Myaspect {
@Pointcut("execution(* com.gfxy.aspect.Log.*(..))")
public void pointcut(){
}
@Before("pointcut()")
public void myBefore(JoinPoint jp) {
System.out.print("前置"+"函数名:"+jp.getSignature().getName()+"参数名:"+ Arrays.toString(jp.getArgs()));
}
@After("pointcut()")
public void myafter( JoinPoint jp) {
System.out.print("后置"+"函数名"+jp.getSignature().getName());
}
@Around("pointcut()")
public void around(ProceedingJoinPoint pj){
System.out.print("目标方法:"+pj.getSignature().getName()+"#这是环绕置通知");
}
}
3.配置spring-aop.xml
4.测试
四.使用注解实现声明式事物1.添加maven依赖
mysql
mysql-connector-java
8.0.11
org.springframework
spring-jdbc
4.2.0.RELEASE
org.springframework
spring-tx
4.2.0.RELEASE
2.创建普通类和方法(使用@Transactional注解)
@Transactional
public class JT {
private JdbcTemplate jt;
public void addUser(int id,String name){
this.jt.update("update user set name =? where id=1");
}
3.配置spring-jdbc.xml
>
4.测试
-
public class test1 { public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("spring-jdbc.xml"); JT jt= (JT) context.getBean("jt"); jt.addUser(1,"mvc"); System.out.println("修改成功"); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)