Spring的DI注入方式:注解注入

/* * 环境:使用注解需要导入aop的jar包(spring.aop.jar),因为注解底层用到了 * 注解无需set,但set不可缺少,因为可能其他地方需要用到 * 常用注解: * 四者作用并无区别,只是用于区分不同的模块 * 但要使用getbean时,就需要使用@Component * @Component(“value”):组件 * @Repository(“value”): * @Service(“value”): * @Controller(“value”): * * @Value(“value”):值 * @Autowird:默认byType自动注入,如果没有则再按byName自动注入 * @Qualifier(“value”):与@Autowird配合使用,指定byname自动注入 * @Resource:默认按bytype自动注入,如果@Resource(name=“value”),则说明指定为byname自动注入 * @PostConstruct:定义bean生命周期的始(初始化完毕后),与原来的init-method等效 * @PreDestroy:定义bean生命周期的末(销毁之前),与原来的destroy-method等效 * 了解: * @Configuration:将有这个标志的当成spring容器使用,可以替换application.xml容器 * @Bean:代表其是一个bean标签,属性之一:name:跟标签中一样 */
String resource = “applicationContext.xml”; ApplicationContext applicationContext=new ClassPathXmlApplicationContext(resource); Student student = (Student) applicationContext.getBean(“ProStudentBean”); System.out.println(student); //Student [studentId=null, classId=1, studentname=张三, gender=男, school=School [school=广东大学]]

猜你喜欢

转载自blog.csdn.net/weixin_44543131/article/details/113173117