我的方面课程将是,
@Configuration
@EnableAspectJAutoProxy
@Component
@Aspect
public class AspectClass {
@Before("execution(* com.pointel.aop.test1.AopTest.beforeAspect())")
public void logBefore(JoinPoint joinPoint) {
System.out.println("Before running the beforeAspect() in the AopTest.java class!");
System.out.println("Hijacked Method name : " + joinPoint.getSignature().getName());
System.out.println("************************");
}
}
Run Code Online (Sandbox Code Playgroud)
我的另一个java类
public class AopTest {
public void beforeAspect() {
System.out.println("This is beforeAspect() !");
}
}
Run Code Online (Sandbox Code Playgroud)
我的主要课程是
public class MainMethod {
public static void main(String[] args) {
ApplicationContext context = new FileSystemXmlApplicationContext("ApplicationContext/applicationContext.xml");
AopTest test = (AopTest)context.getBean("bean1");
test.beforeAspect();
}
}
Run Code Online (Sandbox Code Playgroud)
我的 applicationContext.xml 是,
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" …Run Code Online (Sandbox Code Playgroud) 我是第一次学习 Spring AOP。
在此之后,我做了下一个课程
主要类:
public class App {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(AppConfig.class);
context.refresh();
MessagePrinter printer = context.getBean(MessagePrinter.class);
System.out.println(printer.getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)
应用配置类:
@Configuration
@ComponentScan("com.pjcom.springaop")
@EnableAspectJAutoProxy(proxyTargetClass=true)
public class AppConfig {
@PostConstruct
public void doAlert() {
System.out.println("Application done.");
}
}
Run Code Online (Sandbox Code Playgroud)
方面类:
@Component
@Aspect
public class AspectMonitor {
@Before("execution(* com.pjcom.springaop.message.impl.MessagePrinter.getMessage(..))")
public void beforeMessagePointCut(JoinPoint joinPoint) {
System.out.println("Monitorizando Mensaje.");
}
}
Run Code Online (Sandbox Code Playgroud)
和别的...
就像那个应用程序运行良好一样,但是如果我将 proxyTargetClass 设置为 false。然后我得到下面的错误。
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying …Run Code Online (Sandbox Code Playgroud) 我正在使用 Spring 框架 4。
我有一个类(例如 ClassA),其中使用了另一个类(例如 ClassB)。ClassA 的成员变量从 ClassB 获取值。ClassB 有一个静态方法,可以从属性文件中读取数据。ApplicationContext在 ClassB 中,使用注释注入静态成员变量@Autowired。
我想要的是,我想确保当 ClassA 使用其成员变量时,它应该使用从属性文件读取的值进行所有设置。为此,ClassB 应该做好ApplicationContext一切准备以读取MessageSource.
由于 ClassA 被标记为@Component,Spring 加载了 ClassA,但是当它尝试初始化成员变量时,它得到NullPointerException,因为ApplicationContext尚未初始化。
所以我的问题是,有没有什么方法可以让 Spring 告诉某些 bean 应该按某种顺序或类似的方式初始化。我尝试使用@DependsOn注释并指定@Bean给 的 getter 方法ApplicationContext。但它给出了以下例外:
Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'applicationContext': Requested bean is currently in creation: Is there an unresolvable circular reference?
对这个问题有什么想法吗?
谢谢
我想在 Spring 3 mvc 中做 @CustomFilter 注释,如下所示:
@CustomFilter
@RequestMapping("/{id}")
public Account retrieve(@PathVariable Long id) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
(假设升级到 Spring 4 受到限制)目前我必须对 Spring 3 做的事情如下所示:
public class CustomFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
HttpServletRequest request = (HttpServletRequest) req;
....
chain.doFilter(req, res);
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:如何在 Spring 3 MVC 中执行触发过滤器的 @CustomAnnotation 注释?
@Configuration我正在尝试使用@PropertySource和变量在 Spring java 类中加载 config.proprties 文件数据Environment。
示例:http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/PropertySource.html 问题是,我有一个属性,其值如下:
serverName = abc\xyz
Run Code Online (Sandbox Code Playgroud)
当我使用方法读取这个属性时,
String server= env.getProprty("serverName");
System.out.print(server);
Run Code Online (Sandbox Code Playgroud)
值打印为“abcxyz”。
请注意,我尝试使用双反斜杠,例如
serverName = abc\\xyz
Run Code Online (Sandbox Code Playgroud)
但它仍然只是忽略值字符串中的 \ 。另外,我不能使用正斜杠代替反斜杠。
你能帮我修复它吗?提前致谢!!
根据Spring 的文档,我添加了一个关闭钩子:
SpringApplication app = new SpringApplication(App.class);
DefaultProfileUtil.addDefaultProfile(app);
appContext = app.run(args);
appContext.registerShutdownHook();
Run Code Online (Sandbox Code Playgroud)
但是,@PreDestroy如果应用程序在启动后进行编辑,则不会调用该方法kill。
import org.springframework.stereotype.Service;
import javax.annotation.PreDestroy;
import javax.annotation.PostConstruct;
@Service
public class Processor {
public Processor() {
...
}
@PostConstruct
public void init() {
System.err.println("processor started");
}
//not called reliably
@PreDestroy
public void shutdown() {
System.err.println("starting shutdown");
try {Thread.sleep(1000*10);} catch (InterruptedException e) {e.printStackTrace();}
System.err.println("shutdown completed properly");
}
}
Run Code Online (Sandbox Code Playgroud)
我所看到的只是processor started...
processor started
^C
Run Code Online (Sandbox Code Playgroud)
如果我等待至少 30 秒让 spring 完成启动,然后执行kill该过程,那么@PreDestroy带注释的函数就会被调用。
processor …Run Code Online (Sandbox Code Playgroud) 虽然网上每个页面都说@RestController是@Component的规范,但我不知道它是否与DispatcherServlet有关。但是当我通过在 @RestController 和 @Component 之间切换来尝试下面的代码时,我没有看到相同的行为:
首先我尝试使用@RestController:
@RestComponent
public class TestController {
@RequestMapping(value="/testController", method=RequestMethod.POST,consumes=MediaType.APPLICATION_JSON_VALUE)
public void testController() {
System.out.println("Hello");
}
}
Run Code Online (Sandbox Code Playgroud)
我在控制台中得到以下输出:
你好
其次我尝试使用@Component + @ResponseBody:
@Component
@ResponseBody
public class TestController {
@RequestMapping(value="/testController", method=RequestMethod.POST,consumes=MediaType.APPLICATION_JSON_VALUE)
public void testController() {
System.out.println("Hello");
}
}
Run Code Online (Sandbox Code Playgroud)
我在邮递员上收到错误:
{
"timestamp": 1570998345860,
"status": 405,
"error": "Method Not Allowed",
"message": "Request method 'POST' not supported",
"path": "/testController"
}
Run Code Online (Sandbox Code Playgroud)
如果两个注释相同,那么为什么输出存在差异?
下面是 @RestController 和 @Controller 的源代码,可以看出 @RestController 和 @Controller 都是 @Component 的规范:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController { …Run Code Online (Sandbox Code Playgroud) 我正在尝试@Enable为 Spring 框架编写自己的注释,应按如下方式使用:
package com.example.package.app;
@SpringBootApplication
@com.example.annotations.EnableCustom("com.example.package.custom")
public class MyApplication {}
Run Code Online (Sandbox Code Playgroud)
我使用自定义注释遵循Component scan,但这带来了几个限制:
我不能使基本包属性动态化,即我不能传递"com.example.package.base",但需要在配置中预定义包。
我看了看@AliasFor,但无法让它工作。
当我省略基础包时,扫描从注释的定义包开始,而不是从被注释的类的包开始。在上面的例子中,它只会为 中的类扫描和创建 bean com.example.annotations,而不是为com.example.package.*.
我看了一下EntityScanPackages.Registrar.class在@EntityScan注释中导入的是哪个,但它是一个内部类,我的注释无法导入。
一切正常,如果我把@ComponentScan(includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, value = MyAnnotation.class))在MyApplication类,但停止时这个被移动到的元注释工作@EnableCustom。如何告诉 Spring Framework 将其视为@EnableCustom指定@ComponentScan某些默认值的不同方式。我试着元注解注释我用@Configuration,@Component和其他人,但没有成功:
@Configuration
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@ComponentScan(
includeFilters = @ComponentScan.Filter(
type = FilterType.ANNOTATION,
value = ApplicationService.class))
public @interface EnableApplicationServices {
@AliasFor(annotation …Run Code Online (Sandbox Code Playgroud) java spring spring-annotations component-scan spring-autoconfiguration
I am new to Java and working on an existing Spring MVC application. The project has a controller where many endponts are decorated with @Async annotation. But nowhere in the project did I find @EnableAsync annotation.I know @EnableAsync should be placed in one of the @Configuration classes or @SpringBootApplication classes. So I am not sure how the @Async is working of these existing endpoints
@RequestMapping(method= RequestMethod.POST, values= /endpoint1, consumes="application/json"
@Async
Public CompletableFuture<String> resultsForEndpoint1()
{
}
Run Code Online (Sandbox Code Playgroud)
Question1- Can someone please …
我已经搜索了使用@TestConfiguration与@Configuration相比的真正含义,并且我在@TestConfiguration的官方评论中看到了一个声明。
...与常规@Configuration类不同,@TestConfiguration的使用不会阻止@SpringBootConfiguration的自动检测。
该声明的含义是,使用 @Configuration 可以防止自动检测 @SpringBootConfiguration,但 @TestConfiguration 不会。
我对“预防”这个词感到困惑。@Configuration的使用会影响@SpringBootConfiguration吗?
spring spring-annotations spring-boot spring-boot-test spring-autoconfiguration
spring ×8
java ×7
spring-boot ×4
spring-mvc ×3
spring-aop ×2
annotations ×1
aspectj ×1
asynchronous ×1
autowired ×1
spring-web ×1