springmvc @Resource 注解方式问题
如下代码:在自动注入是采用?@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" />
???</property>
?</bean>
? <bean id="dataSource" ></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"
??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>
private HelloService helloService;
要定义以"helloService"为名称的bean