如下代码:在自动注入是采用 @Autowired 可以 但是为什么使用@Resource不行呢
在网上看了几个@Autowired和@Resource的区别 但还是不明白两个具体不同的配置
HelloController类
public class HelloController implements Controller{
private Logger logger = Logger.getLogger(this.getClass().getName());
private String helloWorld; // 该属性用于获取配置文件中的helloWorld属性
private String viewPage; // 用于获取配置文件中的viewPage属性
@Autowired 为什么不能使用@Resource
private HelloService helloService;
@SuppressWarnings("unchecked")
public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException {
// 在该方法中处理用户请求
Map model = new HashMap();
model.put("helloWorld", getHelloWorld());
String ss=helloService.getId();
//model.put("id",helloService.getId());
//req.setAttribute("ss",ss);
// 将helloWorld属性存入model中
res.getWriter().write(ss);
return null;
// 调用getViewPage获取要返回的页面
}
public void setViewPage(String viewPage){
this.viewPage = viewPage;
}
public String getViewPage(){
return this.viewPage;
}
public void setHelloWorld(String helloWorld){
this.helloWorld = helloWorld;
}
public String getHelloWorld(){
return this.helloWorld;
}
}
HelloDao类
@Repository
public class HelloDao {
static ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//得到数据源
private static DataSource ds=(DataSource) context.getBean("dataSource");
static JdbcTemplate jt1=(JdbcTemplate) context.getBean("jt");
public String getId(){
String sql =" select * from test order by id asc limit 1 ";
Map map =jt1.queryForMap(sql);
System.out.println(""+map.get("id"));
return map.get("id").toString();
}
}
HelloService类
@Service
public class HelloService {
@Autowired 为什么不能使用@Resource
private HelloDao helleDao;
@Cacheable(modelId="testCaching")
public String getId(){
helleDao.getId();
return helleDao.getId();
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:ehcache="http://www.springmodules.org/schema/ehcache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springmodules.org/schema/ehcache
http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
" default-autowire="byName">
<context:annotation-config/>
<bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref local="dataSource" />
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>root</value>
</property>
</bean>
<context:component-scan base-package="test.t" ></context:component-scan>
<ehcache:config configLocation="classpath:ehcache.xml" id="cacheProvider"/>
<ehcache:annotations providerId="cacheProvider">
<ehcache:caching cacheName="testCache" id="testCaching"/>
<ehcache:flushing cacheNames="testCache" id="testFlushing" />
</ehcache:annotations>
</beans>
dispatcherServlet-servlet.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver">
</bean>
<!--配置控制器的映射-->
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="helloWorld.do">helloWorldAction</prop>
</props>
</property>
</bean>
<!--配置视图-->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>
org.springframework.web.servlet.view.InternalResourceView
</value>
</property>
</bean>
<!--指定控制器的实现类,并且配置其参数的值-->
<bean id="helloWorldAction"
class="test.t.HelloController">
<property name="helloWorld">
<value>Hello Spring World!</value>
</property>
<property name="viewPage">
<value>sayHello.jsp</value>
</property>
</bean>
<import resource="classpath:applicationContext.xml"/>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>simpleservlet</servlet-name>
<servlet-class>test.SimpleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>simpleservlet</servlet-name>
<url-pattern>/simpeservlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
</web-app>
分享到:
相关推荐
@Resource和@Autowired都是做bean的注入时使用,其实@Resource并不是Spring的注解,它的包是javax.annotation.Resource,需要导入,但是Spring支持该注解的注入。 1、共同点 两者都可以写在字段和setter方法上。两者...
注解介绍等详细说明及使用: • @Controller • @Service • @Autowired • @RequestMapping • @RequestParam • @ModelAttribute • @Cacheable • @CacheFlush • @Resource • @PostConstruct • @...
在火狐中显示可能会有问题,大家都是程序员,改个参数就好啦 注解包含: 拦截器 , 过滤器 , 序列化 , @After , @AfterReturning , @AfterThrowing , @annotation , @Around , @Aspect , @Autowired , @Bean , @Before ,...
注解配置 org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean 配置形式: org.springframework.orm.hibernate3.LocalSessionFactoryBean --> <value>...
... 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 ... 注解配置 ... <mvc:resources mapping="/resource/**" location="/resource/" /> **" location="/jsp/" /> </beans>
后遗症解决方案:PermissionFilter.java 50行处 解决了这个问题,详情请看代码和注释,其实就是replace 了一下。 HttpServletRequest httpRequest = ((HttpServletRequest)request); /** * 此处是改版后,为了...
掌握SpringMVC的项目整合配置,@Controller,@RequestMapping,@Resource,@PathVariable,@ResponseBody,@ModelAttribute,@CookieValue,@Transactional等注解的使用,json数据传值,国际化,拦截器,权限控制,...
@Resource:JSR-250提供的注解 Java配置 @Configuration声明当前类是一个配置类 @Bean注解在方法上,声明当前方法的返回值为一个Bean AOP @Aspect 声明是一个切面 拦截规则@After @Before ...
通过运行路径 resource/sql/MySQL_script.sql 上的给定 DB 脚本文件来创建数据库模式。 启动命令提示符并转到 checkout code repo 文件夹 运行 mvn clean install 命令来构建应用程序。 将创建的 war 文件部署到 ...
作为常规的来说,一个ssm框架整合,拿maven来说,首先在src/main/resource下加入jdbc.properties,spring- mvc.xml,spring-mybatis.xml等,还有要再web.xml配置监听类和前端控制器,同时还要配置对应的加载spring- ...
它提供了类似于SpringMVC的注释,并且如果您熟悉SpringMVC,则可以很快地掌握它。 静态html网站部署。 动态http api部署。 反向代理服务器。 网络服务器 部署Web服务器: Server server = AndServer . web...
springmvc 框架整合 <!-- 数据库配置 --> <?xml version="1.0" encoding="UTF-8"?> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:...