Spring 注解
使用注解实现IOC
注解的方式将bean 的定义信息和bean实现类结合在一起
@Component:实现bean组件的定义
@Repository:标注dao类
@Service:标注Service 业务类
@Controller:标注控制类
使用@Autowired注解 实现bean的自动装备,默认按类型匹配,可以使用@Qualifier指定bean的名称
1 可以对类的成员变量进行标注
2 也可以对方法的入参进行标注
使用注解的配置信息启动Spring容器
<context:component-scan base-package="cn.kitty.bean"></context:component-scan>
context:命名空间下的 component-scan :扫描注解标注的类 base-package:指定需要的扫描的包 (包与包之间用逗号隔开)
@Component("car")
public class Car {
@Value("blue")
private String color;
public Car() {
}
@Override
public String toString() {
return "Car{" +
"color='" + color + '\'' +
'}';
}
public Car(String color) {
this.color = color;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
@Component("student")
public class Student {
@Value("12")
private int age;
@Value("kitty")
private String name;
/* @Resource(name="car")*///byname
/*@Autowired
@Qualifier("car")*/
@Resource//bytype
private Car car;
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public Student() {
}
public Student(int age, String name , Car car) {
this.age = age;
this.name = name;
this.car = car;
}
@Override
public String toString() {
return "Student{" +
"age=" + age +
", name='" + name + '\'' +
", car=" + car +
'}';
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = 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:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--配置包扫描器--> <context:component-scan base-package="cn.kitty.bean"></context:component-scan> </beans>
test
public class TestZHUJIE { @Test public void TestZHUJIE(){ ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext.xml"); Student student = (Student) context.getBean("student"); System.out.println(student); } }
使用注解来定义切面
使用注解来实现日志切面
使用注解定义前置增强和后置增强
@Aspect
@Before
@AfterReturning
编写配置文件完成切面织入
<aop:aspectj -autoproxy /> 启动对于@Aspectj注解的支持
接口
public interface UserService { public void select(); public void update(); public void insert(); public void delete(); }
实现类
public class UserServiceImpl implements UserService { public void select() { /* int result=5/0;*/ System.out.println("select- "); } public void update() { System.out.println("update--- "); } public void insert() { System.out.println("insert-- "); } public void delete() { System.out.println("delete-- "); } }
增强类Aspectj
@Aspect//打上@Aspect的类就是增强类 不用再实现 xxxxx public class Aspectj { @Before("execution(* zhujie.*.select(..))") public void beforeAdvice(){ System.out.println("------before----"); } @AfterReturning("execution(public void zhujie.UserServiceImpl.select(..))") public void afterAdvice(){ System.out.println("------after----"); }
ApplicationContest.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--目标对象--> <bean id ="userservice" class="zhujie.UserServiceImpl"></bean> <!--增强,通知Advice--> <bean class="zhujie.Aspectj"></bean> <!--aspectj 自动代理--> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
test类
public class AspectjTest { public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("aspectj.xml"); UserService service= (UserService) context.getBean("userservice"); service.delete(); service.insert(); service.update(); service.select(); } }

浙公网安备 33010602011771号