Tag: spring annotations

Spring MVC。 加载上下文时不识别RequestMapping注释名称属性

我有一个带有@RequestMapping注释的Spring @Controller的应用程序。 使用Spring(core和webmvc)4.1.0.RELEASE或更高版本我在启动app服务器时遇到exception。 java.lang.NoSuchMethodError: org.springframework.web.bind.annotation.RequestMapping.name()Ljava/lang/String; at method RequestMappingHandlerMapping.createRequestMappingInfo(RequestMapping annotation, RequestCondition customCondition) 使用Eclipse检查进行调试时,方法变量annotation不包含name属性,并且在操作annotation.name()失败。 所有其他属性值都是正确的(它们具有在使用@RequestMapping注释的Controller方法中定义的值)。 我正在使用Tomcat 7和Maven 2.3。 mvn dependency:tree不显示其他Spring版本而不是4.1.0.RELEASE。 使用Spring 4.0.7.RELEASE或更低版本时不会发生错误。 有没有人有任何线索?

如何配置@ComponentScan动态?

@ComponentScan( //CS1 basePackages = {“com.package.A”, “com.package.B”}, excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {com.Package.A.SomeClass.class }) ) @ComponentScan( //CS2 basePackages = { “com.package.A”} ) @EnableAutoConfiguration @SpringBootApplication public class Application { public static void main(String[] args) throws Exception { ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args); } } 上面是我的SpringBootApplication的主类。 如您所见,我必须使用Annnotation,而不是xml 。 有两个@ComponentScan Annotations.And当然不允许使用它。 对我来说,两个不同的@ComponentScan意味着启动我的应用程序的两种不同方式。 如果我选择使用CS1(意思是@ ComponentScan1),我就会评论CS2,反之亦然。 它不优雅或优雅。特别适合那些为spring新手。 所以我想知道如何根据我的.properties文件将其配置为动态 。例如我的.properties文件中名为“isScanA”的param是真的,那么我可以使用CS1。 […]

如果我在配置类上使用@ActiveProfiles注释而不是在定义我的bean的类上使用它,那么在Spring中会发生什么?

我正在攻读Spring Core认证,我对将配置文件用于JUnit测试有一些疑问。 所以我知道如果我按以下方式编写一个类: @Profile(“stub”) @Repository public class StubAccountRepository implements AccountRepository { private Logger logger = Logger.getLogger(StubAccountRepository.class); private Map accountsByCreditCard = new HashMap(); /** * Creates a single test account with two beneficiaries. Also logs creation * so we know which repository we are using. */ public StubAccountRepository() { logger.info(“Creating ” + getClass().getSimpleName()); Account account = […]

如何使用自定义参数注释返回自定义弹簧错误响应?

我刚开始spring,所以请原谅我的无知。 当给定的方法参数(“required”)为null时,我试图“返回”自定义响应。 从spring开始,目前的反应是: { “timestamp”: 1477060294961, “status”: 400, “error”: “Bad Request”, “exception”: “org.springframework.web.bind.MissingServletRequestParameterException”, “message”: “Required String parameter ‘bookname’ is not present”, “path”: “/getbook” } 我试图达到“返回”的程度: { “status”: 400, “error”: { // custom error body } } 我认为这样做的好方法是使用自定义“参数注释”。 这也可以使代码更具可读性并存储有关此端点参数的有用信息。 我按照这里给出的示例,但我不确定在何处或如何返回自定义响应? 到目前为止我有注释: @Target(ElementType.PARAMETER) @Retention(RetentionPolicy.RUNTIME) @Component public @interface customParameter { String value() default “”; boolean required() default false; […]

Tuckey URL重写filterJava类配置

我一直在研究如何在Tomcat 8上执行URL重写并继续运行相同的两个建议。 1)使用Tuckey URLRewriteFilter。 2)在Tomcat之上运行Apache以使用mod_rewrite。 关于前者,URLRewriteFilter似乎没有任何关于如何以Java格式配置而不是XML的文档。 我的Spring MVC应用程序没有使用web.xml文件 – 所有配置都是通过Java类完成的 – 因此我无法使用XML进行配置。 有没有办法以这种方式配置,或者除了尝试在Tomcat之上运行Apache之外还有什么好的选择吗? 例如,有没有办法在Java中实现这一点而不是XML: UrlRewriteFilter org.tuckey.web.filters.urlrewrite.UrlRewriteFilter UrlRewriteFilter /* REQUEST FORWARD 这是我的WebApplicationInitializer类: public class Initializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext container) { // Create the ‘root’ Spring application context AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.register(RootConfig.class); // Manage the lifecycle of the root application context […]

在spring mvc中创建自定义注释并获取httpservletrequest对象

我想创建自定义注释并使用HttpServletRequest对象将该注释放在方法级别上。 到目前为止我这样做了: 创建注释 @Target(value={ElementType.METHOD,ElementType.PARAMETER}) @Retention(value=RetentionPolicy.RUNTIME) @Documented @Inherited @Mapping public @interface CheckSession{ boolean isAuthenticate() default false; } 创建处理程序类 @Component public class CheckSessionClass implements HandlerMethodReturnValueHandler,HandlerMethodArgumentResolver { @Override public Object resolveArgument(MethodParameter arg0, ModelAndViewContainer arg1, NativeWebRequest arg2, WebDataBinderFactory arg3) throws Exception { logger.info(“……MY ANNOTATION CALLEDD…..resolveArgument”); return null; } @Override public boolean supportsParameter(MethodParameter arg0) { logger.info(“……MY ANNOTATION CALLEDD…..supportsParameter”); return false; […]

使用Spring @Configuration批注注入bean列表

我有一个Spring bean,在Spring Bean中我依赖于其他bean的列表。 我的问题是:如何将bean的通用列表注入该bean的依赖项? 例如,一些代码: public interface Color { } public class Red implements Color { } public class Blue implements Color { } 我的豆子: public class Painter { private List colors; @Resource public void setColors(List colors) { this.colors = colors; } } @Configuration public class MyConfiguration { @Bean public Red red() { return new […]

@PostFilter和@PreFilter如何工作

作为spring注释的新手,我需要澄清下面的代码。 @PostFilter(“hasPermission(filterObject, ‘READ’) or hasRole(‘ROLE_ADMIN’)”) public List getUsers(String orderByInsertionDate, Integer numberDaysToLookBack) throws AppException ; 所以这意味着getUsers返回的用户列表将只包含对调用对象具有完全”READ”访问权限的元素,或者调用对象具有”ROLE_ADMIN”角色。 谢谢。

如何在使用注释时声明Spring bean autowire-candidate =“false”?

我正在使用@ComponentScan和@Component来定义我的spring bean。 我想要的是将这些bean中的一个声明为autowire-candidate=false 。 这可以使用xml中的此属性来完成。 是不是注释中的等价物? 我想要这个的原因是因为我有2个相同接口的实现,我不想使用@Qualifier 。 编辑:使用@Primary是一个有效的解决方案,但autowire候选人在我看来似乎是一个有用的function与自己的语义。 谢谢

是否可以在Spring Framework中使用注释设置bean名称?

我有这样一个bean: @Bean public String myBean(){ return “My bean”; } 我想自动assembly它: @Autowired @Qualifier(“myBean”) public void setMyBean(String myBean){ this.myBean=myBean; } 我需要这样的东西: @Bean(name=”myCustomBean”) 是否可以使用开箱即用的bean自定义名称? 如果不可能开箱即可,那么如何创建这样的bean呢?