1、IOC注解
1.1 IOC和DI的注解
IOC:
@Component:实现Bean组件的定义
@Repository:用于标注DAO类,功能与@Component作用相当
@Service:用于标注业务类
@Controller:用于标注控制器
DI:
@Resource(name="userService")默认ByName方式,如果name确实默认按照ByType方式注入
@Autowired,默认ByType方式,如果出现同名类,则不能按照Type进行注入,需要使用@Qualifier 指明ID
1.2 IOC注解实现添加用户案例
(1)实体类
package cn.spring.ioc.entity; import java.io.Serializable; public class UserInfo implements Serializable { private Integer user_id; private String user_name; public Integer getUser_id() { return user_id; } public void setUser_id(Integer user_id) { this.user_id = user_id; } public String getUser_name() { return user_name; } public void setUser_name(String user_name) { this.user_name = user_name; } }
(2)ApplicationContext.xml
(最好先去完成ApplicationContext配置文件,以免忘记)
<!--扫描注解:包扫描器--> <context:component-scan base-package="cn.spring"></context:component-scan>
(3)IUserInfoMapper接口
package cn.spring.ioc.mapper; import cn.spring.ioc.entity.UserInfo; public interface IUserInfoMapper { public int addUser(UserInfo info); }
(4)IUserInfoMapperImpl接口实现类
package cn.spring.ioc.mapper.impl; import cn.spring.ioc.entity.UserInfo; import cn.spring.ioc.mapper.IUserInfoMapper; import org.springframework.stereotype.Repository; /** * dao层标识 @Repository */ @Repository public class IUserInfoMapperImpl implements IUserInfoMapper { @Override public int addUser(UserInfo info) { System.out.println("添加成功!"); return 1; } }
(5)IUserInfoService
package cn.spring.ioc.service; import cn.spring.ioc.entity.UserInfo; public interface IUserInfoService { public int addUser(UserInfo info); }
(6)IUserInfoServiceImpl实现类
package cn.spring.ioc.service; import cn.spring.ioc.entity.UserInfo; public interface IUserInfoService { public int addUser(UserInfo info); }
(7)测试类
package cn.spring; import cn.spring.ioc.entity.UserInfo; import cn.spring.ioc.service.IUserInfoService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class IocTest { public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); IUserInfoService bean = (IUserInfoService)context.getBean("iUserInfoServiceImpl"); bean.addUser(new UserInfo()); } }
(8)控制台
2、AOP注解
2.1 实现AOP的注解
@Aspect 声明切面
@Ponitcut 声明公共的切点表达式
@Before 前置增强
@AfterReturning 后置增强
@Around 环绕增强
@AfterThrowing 异常抛出增强
@After 最终增强
2.2 AOP注解实现前后置增强
(1)IdoSomeService层
package cn.spring.aop; import org.springframework.stereotype.Service; /** * 业务类 */ @Service("idoSomeService") public class IdoSomeService { public void doSome(){ System.out.println("业务类当中的doSome方法"); } public void say(){ System.out.println("业务类当中的say方法"); } }
(2)MyAdvice增强类
package cn.spring.aop; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; /** * 增强类,把增强类注入到Spring容器 */ @Aspect @Component public class MyAdvice { //定义一个空方法,为了可以应用切点表达式 @Pointcut("execution(* *..aop.*.*(..))") public void pointCut(){} //自定义增强方法 @Before("pointCut()") public void before(){ System.out.println("=====前置增强====="); } //自定义增强方法 @AfterReturning("pointCut()") public void after(){ System.out.println("=====后置增强====="); } }
(3)applicationContext.xml配置文件
<!--扫描注解:包扫描器--> <context:component-scan base-package="cn.spring"></context:component-scan> <!--开启AOP注解支持--> <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
(4)测试类
package cn.spring; import cn.spring.aop.IdoSomeService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class AopTest { public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); IdoSomeService idoSomeService = (IdoSomeService)context.getBean("idoSomeService"); idoSomeService.doSome(); idoSomeService.say(); } }
(5)控制台