spring 自定义注解加载

搜索网上一些资料,想达到预期效果总有些问题,有些是配置没有贴出来,导致运行没有达到预期效果。

实现:spring 自定义@Controller注解的加载

1 定义MyController注解

@Component
@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyController {

	String value() default "";
}


2 定义一个Controller

@MyController
@RequestMapping("/discovery1")
public class DiscoveryController {
	
  public DiscoveryController(){
     System.out.println("DiscoveryController");
  }
	
  @RequestMapping(value = "/jdDiscoveryFloorWithList", method = {RequestMethod.POST,RequestMethod.GET})
  public void jdDiscoveryFloorWithList(HttpServletRequest request, HttpServletResponse response){
  System.out.println("自定义Controller已生效");
  }
}


3 定义注解扫描

在xml配置中,添加:
<context:component-scan base-package="com.javasee.**.controller" use-default-filters="false">
<context:include-filter type="annotation" expression="com.javasee.anno.MyListenerController"/>
</context:component-scan>


4 访问

启动web服务器,访问 http://localhost/项目名称/discovery1/jdDiscoveryFloorWithList

如:
http://localhost/javasee-webapp/discovery1/jdDiscoveryFloorWithList

控制台,打印:自定义Controller已生效



可以参考这篇文章:
http://www.cnblogs.com/wcongcode/p/5482239.html
不过,漏了一些配置。

猜你喜欢

转载自angie.iteye.com/blog/2328987