<?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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd "> <context:annotation-config></context:annotation-config> <bean id="oracleDao" class="dao.OracleDao"></bean> <bean id="anoService" class="service.AnoService"></bean> </beans>
先上配置文件,发现了吧<context:annotation-config/>这个东西,配合
xmlns:context="http://www.springframework.org/schema/context" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
才能开启注解注入模式。上代码:
业务bean
package service; import javax.annotation.Resource; import dao.OracleDao; public class AnoService { @Resource private OracleDao oracleDao; public void save() { oracleDao.save(); } }
模拟OracleDao
package dao; public class OracleDao { public void save() { System.out.println("模拟Oracle的保存方法"); } }
junit测试类
package test; import static org.junit.Assert.*; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import service.AnoService; public class TestAnoService { @Test public void test() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); AnoService anoService = (AnoService)applicationContext.getBean("anoService"); anoService.save(); } }
结果:
2014-3-3 21:24:06 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@41d05d: display name [org.springframework.context.support.ClassPathXmlApplicationContext@41d05d]; startup date [Mon Mar 03 21:24:06 CST 2014]; root of context hierarchy
2014-3-3 21:24:06 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
2014-3-3 21:24:06 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@41d05d]: org.springframework.beans.factory.support.DefaultListableBeanFactory@15aed57
2014-3-3 21:24:06 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@15aed57: defining beans [org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,oracleDao,anoService]; root of factory hierarchy
模拟Oracle的保存方法
乐了吧,成功了!
总结一下:
- 配置文件得开启注解注入模式
- 使用注入的类(例如:AnoService)你也得交给容器管理,你自己new出来他,容器是不会给你注入的,原理是Spring在初始化AnoService的时候发现里面有需要注入的对象,容器就把这个对象注入进去,所以你自己new的对象容器是不知道他需要注入的。
相关推荐
2. **XML配置问题**:确保你的Spring配置文件(如`applicationContext.xml`)正确包含了`<context:component-scan>`或`<context:annotation-config>`元素,它们是启用注解配置的关键。 3. **编译器设置**:检查你的...
10. <context:annotation-config /> 11. <!-- 把标记了@Controller注解的类转换为bean --> 12. <context:component-scan base-package="com.mvc.controller" /> 13. <!-- 启动Spring MVC的注解功能,...
`<context:annotation-config/>` 和 `<context:component-scan base-package="需要实现注入的类所在包"/>` 是两个重要的 XML 配置元素,它们用于开启注解支持和指定扫描的包范围。 - `<context:annotation-config/>...
<context:annotation-config /> ``` 这将隐式地向 Spring 容器注册 `AutowiredAnnotationBeanPostProcessor`、`CommonAnnotationBeanPostProcessor`、`PersistenceAnnotationBeanPostProcessor` 和 `...
- 使用`<context:component-scan>`标签扫描控制器所在的包。 - 配置`InternalResourceViewResolver`用于解析视图。 ```xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=...
`<context:annotation-config>`元素会扫描容器中的所有Bean,查找并处理如`@Autowired`、`@Required`、`@PostConstruct`等注解,实现依赖注入。 `<context:component-scan>`元素用于指定需要扫描的包,这样Spring会...
<context:annotation-config /> ``` 这将隐式地向 Spring 容器注册 `AutowiredAnnotationBeanPostProcessor`、`CommonAnnotationBeanPostProcessor`、`PersistenceAnnotationBeanPostProcessor` 和 `...
`<context:annotation-config/>` 元素用于激活注解驱动的 Bean, `<context:component-scan>` 元素用于自动扫描指定包下的 Bean。 最后,我们可以编写主类来测试: ```java @Service public class Main { @...
- **配置DWR Annotation Config**:利用`<dwr:annotation-config/>`标签启用注解支持,使得开发者能够使用`@RemoteProxy`和`@RemoteMethod`等注解来标注需要暴露给客户端的方法。 ```xml <!-- 配置DWR注解支持 -->...
### Spring基于注释...`<context:annotation-config/>`是Spring提供的一个配置选项,它可以自动注册多个BeanPostProcessor,包括`AutowiredAnnotationBeanPostProcessor`、`CommonAnnotationBeanPostProcessor`、`...
本文将详细介绍Spring 2.5及其后续版本中的注解配置方式,包括常见的注解如`@Autowired`、`@Resource`以及如何使用`<context:annotation-config/>`简化配置等。 #### Spring 2.5 的注释 Spring 2.5 版本中,为了更...
<context:annotation-config/> <context:component-scan base-package="com.zhaolongedu"/> ``` - 配置Spring事务管理器: ```xml <bean id="transactionManager" class="org.springframework.orm.hibernate5...
<context:annotation-config/> <context:annotationconfig/> ``` 这两种方式都会隐式地向Spring容器注册四个`BeanPostProcessor`实例:`AutowiredAnnotationBeanPostProcessor`、`...
<param-value>classpath:config/springAnnotation-*.xml</param-value> </context-param> <!-- 启动Spring上下文监听器 --> <listener> <listener-class>org.springframework.web.context....
2. **方式二:使用命名空间`<context:annotation-config/>`** - 这种方式会自动注册多个注解处理器,包括`AutowiredAnnotationBeanPostProcessor`、`CommonAnnotationBeanPostProcessor`、`...
<context:annotation-config /> <context:component-scan base-package="com.mvc" /> <mvc:annotation-driven /> <mvc:resources mapping="/resources/**" location="/resources/" /> <mvc:default-servlet-...
<context:annotation-config /> <context:component-scan base-package="com.mvc.*"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component...
- 使用`<context:annotation-config/>`来启用Spring MVC的注解支持。 - **控制器扫描**: - 通过`<context:component-scan base-package="controller"/>`指定要扫描的控制器包名。 - **视图解析器**: - 配置视图...