启动注解:
<!-- 开启注解权限 --><context:component-scan base-package="com.example.demo.learn.aop"/>
package com.example.demo.learn.aop;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestDemo {private static String path = "config/applicationContext.xml";public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(path);TagService tagService = context.getBean("tagService", TagService.class);tagService.test();}
}
package com.example.demo.learn.aop;import org.springframework.stereotype.Service;@Service
public class TagDao {public void add() {System.out.println("数据库层操作...");}
}
package com.example.demo.learn.aop;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class TagService {@Autowiredprivate TagDao tagDao;public void test() {System.out.println("测试数据.....");tagDao.add();}
}