Springmvc配置优化(@Component+import标签)

1.Springmvc注解方式注入

之前定义统一异常处理类DemoExceptionResolver和切面定义LogAspectj都是配置文件进行配置。

使用@Component表示类,让<context:component-scan base-package="com.company"/>扫入spring容器。

 

@Aspect
@Component
 public class LogAspectj {
    private static final Logger LOGGER = LoggerFactory.getLogger(LogAspectj.class);

    @AfterReturning(value = "@annotation(com.company.aspectj.Log)",returning = "ret")
    public void LogAfterReturning(JoinPoint joinPoint,Object ret){
        Object[] args = joinPoint.getArgs();
        LOGGER.info("方法入参:{}",args);
        LOGGER.info("返回值:{}",ret);
    }
}
@Component
public class DemoExceptionResolver implements HandlerExceptionResolver {
    private static final Logger LOG = LoggerFactory.getLogger(DemoExceptionResolver.class);

    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex){
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");

        PrintWriter pw = null;
        try {
            pw = response.getWriter();
        } catch (IOException e) {
            LOG.error(e.getMessage(),e);
        }

        if (ex instanceof DemoException) {
            pw.write(ex.getMessage());
        } else {
            pw.write("系统异常,请联系管理员");
        }

        return new ModelAndView();
    }
}

同时将spring-mvc.xml相关配置去掉。

 

2.使用import导入其他配置文件

之前其他module下的配置文件是在web.xml配置classpath*:spring-*.xml进加载。

使用import标签进行导入。

  • web.xml:只配置spring-mvc.xml,在spring-mvc.xml使用import标签。
<servlet>
    <description>spring mvc servlet</description>
    <servlet-name>DispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:spring-mvc.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
  • spring-mvc.xml
<!--spring-mvc组件默认配置+启动注解等等-->
<mvc:annotation-driven/>

<context:component-scan base-package="com.company"/>

<!--@aspectj启动,子类代理-->
<aop:aspectj-autoproxy proxy-target-class="true"/>

<!--spring最多加载一个context:property-placeholder-->
<!--如果去掉jdbc.properties加载,demo-web启动会提示找不对应变量值-->
<context:property-placeholder location="classpath:redis.properties,classpath*:jdbc.properties"/>

<!--将spring-redis引入spring中-->
<import resource="spring-redis.xml"/> <import resource="spring-mybatis.xml"/>

 

3.其他变动

demo-base子工程下原来的代码中包名进行修改:将demo=>company。

demo工程地址

 

 

 

转载于:https://my.oschina.net/u/2526015/blog/749511

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
配置一个基于Spring MVC的项目,你可以按照以下步骤进行操作: 1. 创建一个新的Maven或Gradle项目,并确保你已经将相关的依赖项添加到项目的构建文件中。在这种情况下,你需要添加Spring MVC的依赖项。 2. 创建一个 `web.xml` 文件用于配置Servlet容器。在其中,你需要定义一个 `DispatcherServlet`,它将作为前端控制器处理所有的请求。 ```xml <web-app> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> ``` 3. 创建一个 `applicationContext.xml` 文件,该文件将包含Spring MVC的配置。在其中,你需要定义基本的组件扫描、视图解析器、处理器映射器和处理器适配器等。 ```xml <beans xmlns="http://www.springframework.org/schema/beans" 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.xsd"> <!-- 启用组件扫描 --> <context:component-scan base-package="com.example.controller" /> <!-- 启用注解驱动 --> <mvc:annotation-driven /> <!-- 配置视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> </beans> ``` 4. 创建你的控制器类并添加适当的注解,例如 `@Controller` 和 `@RequestMapping`,这样它们就能处理各种不同的请求。 ```java import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class MyController { @RequestMapping(value = "/hello", method = RequestMethod.GET) public String hello() { return "hello"; } } ``` 5. 创建一个视图文件,例如 `hello.jsp`,该文件将用于显示相应的内容。 ```jsp <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Hello</title> </head> <body> <h1>Hello, World!</h1> </body> </html> ``` 6. 运行你的项目,并在浏览器中访问 `http://localhost:8080/hello`,你应该能够看到 "Hello, World!" 的输出。 这些是配置一个基本的Spring MVC项目的基本步骤。当然,你还可以根据自己的需求进行更复杂的配置和定制化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值