一、springmvc注解配置
1.web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>restfull</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring.xml</param-value>
</context-param>
<listener>
<description>spring监听器</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- springMVC核心配置 -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
2.spring.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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd ">
<!-- 开启注解,并扫描注解 -->
<context:component-scan base-package="com.restfull.service"/>
<!-- 加载配置 -->
<context:property-placeholder location="classpath:config.local.properties" />
</beans>
3.spring-mvc.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- 开启注解,并扫描注解 -->
<context:component-scan base-package="com.restfull.controller" />
<!-- 默认的注解映射的支持 -->
<mvc:annotation-driven />
<mvc:default-servlet-handler />
</beans>
4.controller service:
@Controller
@RequestMapping(value="/hehe")
public class WelcomeController {
@Value("${key}")
private String test;
@Autowired
private WelcomeService welcomeService;
private Logger logger = LoggerFactory.getLogger(WelcomeController.class);
@RequestMapping(value="/welcome")
public void welcome(){
logger.info(WelcomeController.class+" "+test);
welcomeService.welcome();
}
}
@Service
public class WelcomeServiceImpl implements WelcomeService {
private Logger logger = LoggerFactory.getLogger(WelcomeServiceImpl.class);
@Value("${key}")
private String test;
@Override
public void welcome(){
logger.info("welcome for you! "+test);
}
}
二、@value使用说明
因为@value注解只能当前上下文中的参数,因此以上配置service上下文中的WelcomeServiceImpl中可以通过@value获取config.local.properties的参数,而controller上下文中的WelcomeController则无法获取