spring mvc注解配置及@value的使用说明

一、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则无法获取

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值