如下代码:在自动注入是采用 @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>