使用AspectJ时,为什么要在@Configurable上使用@Component.
我有@Transactional支持的Spring和AspectJ设置,自我调用的方面以及注入JPA实体.这非常有效.
我正在将@Component用于大多数需要注入的类,因此要么将它们注入到它们的依赖项中.或者,当我不能,注入ApplicationContext然后使用getBean()作为最后的手段.我只为需要注入的JPA实体(Hibernate)保留@Configurable.我也开始使用@Configurable进行jUnit测试,使编写测试变得容易.这也很有效.
但我很好奇.如果AspectJ现在使用@Configurable注释自动注入(bean化)任何东西,无论它是如何构造的; getBean(),new(),@ Autowired.为什么我不能只为我的所有bean切换到使用@Configurable?然后我可以完全取消应用程序上下文和getBean(),只需要new()我无法注入的任何类.
我意识到我没有提到XML bean配置.我并不回避这一点,但这个项目并不需要任何.我只是构造函数或setter在测试时注入依赖项.很容易.
Thinker.java
package springdemo2;
public interface Thinker {
void thinkOfSomething(String thoughts);
}
Run Code Online (Sandbox Code Playgroud)
Volunteer.java
package springdemo2;
public class Volunteer implements Thinker{
private String thoughts;
@Override
public void thinkOfSomething(String thoughts) {
this.thoughts=thoughts;
}
public String getThoughts(){
return thoughts;
}
}
Run Code Online (Sandbox Code Playgroud)
MindReader.java
package springdemo2;
public interface MindReader {
void interceptThoughts(String thoughts);
String getThoughts();
}
Run Code Online (Sandbox Code Playgroud)
Magician.java
package springdemo2;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class Magician implements MindReader {
private String thoughts;
@Pointcut("execution(* springdemo2."
+ "Thinker.thinkOfSomething(String)) and args(thoughts)")
public void thinking(String thoughts){
} …Run Code Online (Sandbox Code Playgroud) 我的项目基于spring framework 2.5.4.我尝试为某些控制器添加方面(我使用aspectj 1.5.3).
我在application-servlet.xml中启用了自动代理,只是将这些行粘贴到xml文件的末尾:
<aop:aspectj-autoproxy />
<bean id="auditLogProcessor" class="com.example.bg.web.utils.AuditLogProcessor" />
Run Code Online (Sandbox Code Playgroud)
创建方面:
package com.example.bg.web.utils;
import org.apache.log4j.Logger;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class AuditLogProcessor
{
private final static Logger log = Logger.getLogger(AuditLogProcessor.class);
@After("execution(* com.example.bg.web.controllers.assets.AssetThumbnailRebuildController.rebuildThumbnail(..))")
public void afterHandleRequest() {
log.info("test111");
}
@After("execution(* com.example.bg.web.controllers.assets.AssetThumbnailRebuildController.rebuildThumbnail(..))")
public void afterRebuildThumbnail() {
log.info("test222");
}
}Run Code Online (Sandbox Code Playgroud)
我的控制器:
class AssetAddController implements Controller
class AssetThumbnailRebuildController extends MultiActionControllerRun Code Online (Sandbox Code Playgroud)
当我在方面顾问和调用控制器中设置制动点时,我只捕获afterHandleRequest()而不是afterRebildThumbnail()我做错了什么?
注意
我代表我的朋友问这个问题,他不能访问SO beta,我也不知道它是什么.
编辑
确实有一些拼写错误,谢谢Cheekysoft.但问题仍然存在.
我想使用@AutoWired将配置了@Component的非托管bean注入托管bean.我很确定我的配置是正确的,但由于某种原因,我一直得到例外:
No unique bean of type [foo.Baz] is defined: Unsatisfied dependency of type [class foo.Baz]: expected at least 1 matching bean
Run Code Online (Sandbox Code Playgroud)
根据错误,我猜它无法找到Baz类,但我不确定为什么.我的理解是上下文:XML配置中的spring配置元素应该允许我这样做.我还确保包含适当的jar文件(spring-weaving.jar和aspectjweaver.jar).
这是我设置的一个简单示例.
我的XML配置:
<beans ...>
...
<context:annotation-config/>
<context:spring-configured/>
<context:component-scan base-package="foo"/>
<bean id="bar" class="foo.Bar"/>
...
</beans>
Run Code Online (Sandbox Code Playgroud)
我有一个托管bean:
package foo;
public class Bar {
@Autowired
private Baz baz;
public void setBaz(Baz baz) {
this.baz = baz;
}
...
}
Run Code Online (Sandbox Code Playgroud)
还有一个非托管bean:
package foo;
@Component
public class Baz {
...
}
Run Code Online (Sandbox Code Playgroud)
有什么我想念的吗?
编辑:日志列出了它实例化的bean,而foo.Baz不是其中之一.我不知道为什么它没有拿起@Component注释类.
我已经为我的应用程序设置了一个加载屏幕(启动),通过一些建议解释了"正确的方法":
styles.xml:
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/splash_vector_drawable</item>
</style>
Run Code Online (Sandbox Code Playgroud)
表现:
<activity
android:name="com.tba.versionc.SplashActivity"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)
Java的:
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
}
Run Code Online (Sandbox Code Playgroud)
当我为启动画面设计矢量drawable时,我设置它的宽高比以匹配我的手机(16:9),它似乎工作正常,但我担心在设备上运行时会出现的宽高比会发生什么屏幕比例不同.
为了消除它"伸展以适应"错误尺寸屏幕的可能性,我宁愿它是中心裁剪,但由于它不是ImageView,甚至是布局文件的一部分,我不知道有什么办法设置中心裁剪属性.
任何人?
我正在使用 m2e 构建一个 java 项目。我需要使用JAVA 版本 1.6。所以我正在尝试配置工具链插件来实现它。通过参考下面的链接。
https://maven.apache.org/guides/mini/guide-using-toolchains.html
但在 Eclipse 中它抛出以下错误。
生命周期配置未涵盖插件执行:org.apache.maven.plugins:maven-toolchains-plugin:1.1:toolchain (execution:default,phase:validate)pom.xml/Replenishment line 98 Maven项目构建生命周期映射问题
我引用了该链接 ,但我没有得到适当的清晰度。下面是用于配置工具链插件的代码片段。
在 pom.XML 中
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-toolchains-plugin</artifactId>
<version>1.1</version>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-toolchains-plugin
</artifactId>
<versionRange>
[1.1,)
</versionRange>
<goals>
<goal>toolchain</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
<executions>
<execution>
<goals>
<goal>toolchain</goal>
</goals>
</execution>
</executions>
<configuration>
<toolchains>
<jdk>
<version>1.6</version>
<vendor>sun</vendor>
</jdk>
</toolchains>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
和我的 toolchains.xml
<?xml version="1.0" encoding="UTF8"?>
<toolchains>
<!-- JDK toolchains -->
<toolchain>
<type>jdk</type>
<provides> …Run Code Online (Sandbox Code Playgroud) 由于某种原因,我的本地dev分支是我的默认发布分支。IntelliJ UI 有什么办法可以改变这个吗?
为了澄清起见,“默认”是指该分支的默认值,而不是所有分支的默认值。所有分支的默认值都是分支名称本身。我不知道我的dev默认值是如何改变的。
我有这个服务/控制器方法:
public ResponseEntity<PolicyDTO> addPolicy(@Valid @RequestBody PolicyDTO policy)
throws InternalServerException, BadRequestException {
log.debug("Adding a new policy");
}
Run Code Online (Sandbox Code Playgroud)
注意方法参数中的@Valid批注
对于此控制器,我们定义了以下方面类:
@Aspect
@Component
public class BackwardsCompatibilityHandler {
@Around("execution(* com.company.PolicyController.addPolicy(..))")
public Object ControllerMethod(JoinPoint jp)
{
// DO Code before and after the method call
}
Run Code Online (Sandbox Code Playgroud)
这方面/代理背后的主要原因是拦截方法调用,并对输入参数/返回的结果进行一些前/后处理。
我们现在面临的主要问题是@Valid批注在实际执行代码之前和之后的方面之前进行处理。
关于如何使验证检查在AOP代码中运行的任何想法?我知道有一种方法可以通过手动调用方法内部的验证器来做到这一点,但是我不想过多地触摸现有代码...因此,除此以外,还有其他任何想法...
现在执行的顺序是:
我们希望有更多类似的东西(首先是我们的代码,然后是Valid注释):
请跳过编译器错误等,该代码仅用于演示目的,不是真正的代码:)
我在 CentOS Linux 版本 7.3.1611 服务器上使用 Jenkins 版本 2.73-1.1。
服务器上有3个不同版本的JDK:
[root @ jenkins java] # ll
total 12
lrwxrwxrwx. 1 root root 16 27 Apr 16.25 default -> / usr / java / latest
drwxr-xr-x. 8 root root 4096 27 Mar 2013 jdk1.6.0_45
drwxr-xr-x. Root root 4096 11 Apr 2015 jdk1.7.0_80
drwxr-xr-x. 9 root root 4096 27 Apr 16.25 jdk1.8.0_131
lrwxrwxrwx. 1 root root 22 27 apr 16.25 latest -> /usr/java/jdk1.8.0_131
Run Code Online (Sandbox Code Playgroud)
如下图所示,Jenkins使用的是jdk1.8.0_131版本,在我的项目中我指定使用JDK7。
当我尝试编译我的项目 ( …
spring ×5
spring-aop ×5
aop ×3
git ×2
java ×2
annotations ×1
aspect-ratio ×1
autowired ×1
components ×1
configurable ×1
drawable ×1
eclipse ×1
gitignore ×1
java-7 ×1
jenkins ×1
maven ×1
pycharm ×1
screen ×1
unmanaged ×1
validation ×1
xml ×1