标签: spring-annotations

Spring Framework中的基于Java的Java配置不起作用

我正在阅读有关基于Spring Java的配置的这篇文章.我正在创建一个类似于本教程中的示例,但我正在使用Maven进行构建过程.

我已经添加了以下依存关系到我的项目:spring-core,spring-bean,spring-context,spring-context-supportspring-asm.

问题是,当我尝试执行我的应用程序时,我收到以下错误消息:

feb 13, 2013 10:26:49 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@4ba4536d: startup date [Wed Feb 13 10:26:49 CET 2013]; root of context hierarchy
Exception in thread "main" java.lang.IllegalStateException: CGLIB is required to process @Configuration classes. Either add CGLIB to the classpath or remove the following @Configuration bean definitions: [helloWorldConfig]
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses(ConfigurationClassPostProcessor.java:327)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanFactory(ConfigurationClassPostProcessor.java:222)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:681)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:620)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:446)
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:73)
    at org.andrea.myexample.myJavaConfiguration.MainApp.main(MainApp.java:13)
Run Code Online (Sandbox Code Playgroud)

在线阅读我已经明白要使用这些注释(@Configuration …

java spring spring-mvc spring-annotations

2
推荐指数
1
解决办法
4641
查看次数

无法解析未找到WebApplicationContext:未注册ContextLoaderListener

我正在尝试仅使用注释创建Spring Web应用程序(没有配置xml文件).我使用tomcat作为应用程序服务器和maven来构建项目.谷歌搜索了很多,修改了很多,但仍然坚持这个错误.请在下面找到文件.希望可以有人帮帮我.提前致谢.:)

下面是pom.xml

   <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany.app</groupId>
    <artifactId>Angular</artifactId>
    <packaging>war</packaging>
    <version>0.1</version>
    <name>Angular Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>3.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-hibernate3</artifactId>
            <version>2.0.8</version>
            <exclusions>
                <exclusion>
                    <artifactId>hibernate</artifactId>
                    <groupId>org.hibernate</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>servlet-api</artifactId>
                    <groupId>javax.servlet</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-commons-core</artifactId>
            <version>1.4.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.2.2</version>
        </dependency>
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>sqljdbc4</artifactId>
            <version>4.0</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>3.6.10.Final</version>
            <optional>true</optional>
            <exclusions>
                <exclusion>
                    <artifactId>asm</artifactId>
                    <groupId>asm</groupId>
                </exclusion>
            </exclusions> …
Run Code Online (Sandbox Code Playgroud)

spring spring-mvc spring-annotations spring-data

2
推荐指数
1
解决办法
2万
查看次数

如何向正则表达式添加点

@Pattern(regexp="^\\w{8,}")
private String username;
Run Code Online (Sandbox Code Playgroud)

该模式只能由数字、字母和下划线字符组成。

如何将点 (.) 添加到图案中

谢谢

regex validation spring-annotations

2
推荐指数
1
解决办法
8608
查看次数

春天:@Value vs. @Autowired

我正在处理的应用程序中使用注入有一些问题(使用Spring Version 3.1.2).首先,我看到很多这样的代码:

@Value("#{searchRequestBean}")
private SearchRequest searchRequest;

@Value("#{searchResponseBean}")
private SearchResponse searchResponse;

@Autowired
private SavedSearchService service;
Run Code Online (Sandbox Code Playgroud)

这三个中的每一个似乎都具有将指定的bean/service自动装配到类中的效果.我不明白的是,有什么之间的区别@Value,并@Autowired在这些情况下?我在网上找到的每个例子似乎都用于@Value从属性文件中注入值.在这种情况下,SearchResponse并且SearchRequest是抽象类.

我希望更好地理解这一点将有助于我解决我对Session bean的一些问题.

java spring dependency-injection autowired spring-annotations

2
推荐指数
1
解决办法
1万
查看次数

在Spring Security中使用PreAuthorize注释中的permitAll()的目的

作为Spring安全框架的新手,我想知道为什么我们使用@PreAuthorize("permitAll()")方法?文档说permitAll总是评估为true.(http://docs.spring.io/spring-security/site/docs/3.0.x/reference/el-access.html)

另外,我有以下代码更改.开发人员从permitAll()更改为特定权限检查.这里的含义是什么?由于我不太确定permitAll()如何工作,我无法判断代码更改背后的逻辑.在我看来,开发人员添加了特定的权限检查,并将null作为身份验证对象传递.有人可以解释显式传递null作为身份验证对象的影响吗?未经身份验证的用户是否具有访问权限,如果他们对目标对象具有此特定的"LUONTI"权限 - 'opetussuunnitelma'?

-    @PreAuthorize("permitAll()")
+    @PreAuthorize("hasPermission(null, 'opetussuunnitelma', 'LUONTI')")
     OpetussuunnitelmaDto addOpetussuunnitelma(OpetussuunnitelmaDto opetussuunnitelmaDto);
Run Code Online (Sandbox Code Playgroud)

谢谢.任何帮助非常感谢!

java security annotations spring-security spring-annotations

2
推荐指数
2
解决办法
1万
查看次数

在Java Spring Integration中,变压器元素可以包含路由器功能吗?

我需要一个组件:

  1. 接收消息,对消息的有效负载和报头进行转换(到目前为止,它就像一个转换器).

  2. 然后,基于在标头路由上传递到适当信道的值(像标题路由器一样).

我想在java而不是XML中纯粹使用注释来配置它,但我绝对会采取我在这方面可以获得的内容.如果有人知道XML解决方案,请传递它.

如果它有帮助,我的用例是我希望变换器在消息有效负载上实现的转换依赖于自定义加载的头值.我还希望用于从变换器发送消息的通道依赖于标头值.

我知道涉及多个转换器的解决方案,每个转换类型一个,如标头值中所定义,然后是路由器.我试图避免这种解决方案,并且最多只使用一个路由器和单个变压器.

谢谢您的帮助.

java spring spring-integration spring-annotations

2
推荐指数
1
解决办法
1535
查看次数

如何动态配置@ComponentScan?

@ComponentScan(  //CS1\n    basePackages = {"com.package.A", "com.package.B"},\n    excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,\n                                           value = {com.Package.A.SomeClass.class\n                                           })\n)\n\n@ComponentScan(  //CS2\n    basePackages = { "com.package.A"}\n)\n@EnableAutoConfiguration\n@SpringBootApplication\npublic class Application {\n  public static void main(String[] args) throws Exception {\n    ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args);\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

上面是我的SpringBootApplication主要课程。正如你所看到的,我必须使用 Annnotation,而不是 xml。有两个@ComponentScanAnnotation,当然Spring是不允许的。对我来说,这两种不同@ComponentScan意味着两种不同的方式来启动我的应用程序。如果我选择使用 CS1(这意味着 @ComponentScan1),我必须注释 CS2\xef\xbc\x8can,反之亦然。

\n\n

它不优雅,不优雅。尤其是对于那些 Spring 新手。所以我想知道如何根据我的.properties文件动态配置它。例如我的.properties文件中名为“isScanA”的参数是true,那么我可以使用CS1。或者任何其他优雅的方式。

\n\n

我已经尝试了很多。

\n\n
    \n
  1. 使用占位符。例如@ComponentScan(basePackage="${scan.basePackage}")。并在需要时更改.properties文件中的值。但这种方法无法修复excludeFilters。因为如果我用来FilterType.ASSIGNABLE_TYPE分配需要排除的类,则value应该是Class类型而不是String,如果value = {"${scan.excludeClass}"}使用的话。

  2. \n …

java spring spring-annotations component-scan

2
推荐指数
1
解决办法
8612
查看次数

处理由表单提交传递到 Spring MVC 控制器方法的“boolean”参数的最聪明方法是什么?

我正在开发一个 Spring MVC 应用程序,我对将布尔参数处理为表单提交发送的POST 请求的更智能方法有以下疑问。

所以我有以下简单的 html 页面,其中包含一个表单:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
     "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Spring MVC - Hibernate File Upload to Database Demo</title>
</head>
<body>
    <div align="center">
        <h1>Spring MVC - Hibernate File Upload to Database Demo</h1>
        <form method="post" action="http://localhost:8080/AccomodationMedia/" enctype="multipart/form-data">
            <table border="0">
                <tr>
                    <td>Pick file #1:</td>
                    <td><input type="file" name="fileUpload" size="50" /></td>
                </tr>
                <tr>
                    <td>Pick file #2:</td>
                    <td><input type="file" name="fileUpload" size="50" /></td>
                </tr>
                <tr>
                    <td colspan="2" align="center"><input type="submit" value="Upload" /></td>
                </tr> …
Run Code Online (Sandbox Code Playgroud)

spring spring-mvc spring-annotations

2
推荐指数
1
解决办法
1万
查看次数

Spring AOP - 带有 args 的注释

我在 Spring Boot 中遇到了问题。我正在尝试为某些 RestControllers 提供额外的功能,并且我正在尝试使用一些自定义注释来实现它。这是一个例子。

我的注释:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyCustomAnnotation {
  String someArg();
}
Run Code Online (Sandbox Code Playgroud)

我的方面:

@Aspect
@Component
public class MyAspect {
  @Around(
    value = "@annotation(MyCustomAnnotation)",
    argNames = "proceedingJoinPoint,someArg"
  )
  public Object addMyLogic(ProceedingJoinPoint proceedingJoinPoint, String someArg)
    throws Throwable
  {
    System.out.println(someArg);
    return proceedingJoinPoint.proceed();
  }
}
Run Code Online (Sandbox Code Playgroud)

我的方法:

@MyCustomAnnotation(someArg = "something")
@GetMapping("/whatever/route")
public SomeCustomResponse endpointAction(@RequestParam Long someId) {
  SomeCustomResult result = someActionDoesNotMatter(someId);
  return new SomeCustomResponse(result);
}
Run Code Online (Sandbox Code Playgroud)

主要基于文档(https://docs.spring.io/spring/docs/3.0.3.RELEASE/spring-framework-reference/html/aop.html - 7.2.4.6 Advice parameters)我很确定,它应该管用。

我在这里,因为它不...

让我发疯的是,即使是 Intellij,当试图帮助处理 argNames(空字符串 -> 红色下划线 -> alt+enter -> 正确的 …

spring-aop spring-annotations spring-boot

2
推荐指数
1
解决办法
3987
查看次数

在java中创建和使用注解

我正在审查开源 Spring 项目。我对这里的注释的使用感到困惑。我想请教一下。

@Target(ElementType.METHOD)
@Retention(RUNTIME)
@Bean
public @interface Merge {

   
    @AliasFor("targetRef")
    String value() default "";
   
    @AliasFor("value")
    String targetRef() default "";

    Placement placement() default Placement.APPEND;
   
    int position() default 0;
    
    Class<MergeBeanStatusProvider> statusProvider() default MergeBeanStatusProvider.class;

    boolean early() default false;

}
Run Code Online (Sandbox Code Playgroud)

此处创建了一个名为 Merge 的注释。它有不同的参数和默认值。

@Configuration
public class LocalConfiguration {

    @Merge(targetRef = "mergedList", early = true)
    public List<String> blLocalMerge() {
        return Arrays.asList("local-config1", "local-config2");
    }
}
Run Code Online (Sandbox Code Playgroud)

这是@Merge我随机选择的任何类中注释的用法。

当我检查代码时,我找不到任何与 Merge 注释的实现相关的类。顺便说一下,我遇到的这个问题不仅仅是关于这个注释。我检查过的几乎所有注释都没有以任何方式实现。如果我们从这个注释开始,我想我会理解其他人。这个注解有什么作用?它给使用它的地方带来了什么样的信息。应用程序如何在没有在任何地方实现的情况下理解该注释在运行时的作用。

谢谢。

java spring spring-annotations spring-boot

2
推荐指数
1
解决办法
80
查看次数