我想使用依赖注入在我的 Grails 应用程序中使用用 Java 编写的服务。在没有注入的情况下用 Java 创建它看起来像这样:
ServiceFactory.newInstance().getElementService()
Run Code Online (Sandbox Code Playgroud)
我想以与为控制器、服务和作业注入服务的方式相同的方式使用它。
class ImportJob {
def elementService
...
}
Run Code Online (Sandbox Code Playgroud)
我知道这应该进入resources.groovy
,这就是我到目前为止所拥有的:
serviceFactory(ServiceFactory) { bean ->
bean.factoryMethod = 'newInstance'
}
elementService(ElementService) {
}
Run Code Online (Sandbox Code Playgroud)
我在文档中发现很少有资源可以帮助解决这个问题。我如何完成 elementService 以便它如上所述创建对象?我应该使用 BeanBuilder 吗?
我已经用自定义注释注释了 Spring bean,但似乎 Spring 在创建 bean 后删除了我的自定义注释。
AnnotatedBean bean = ctx.getBean(AnnotatedBean.class);
Foo.findAndDoStuffWithAnnotatedThings(bean);
Run Code Online (Sandbox Code Playgroud)
第二步不起作用,我的自定义注释丢失了。(可能是由于代理的东西)
我的豆子
@Rule(name = "RoutePickupRule")
@Transactional
@Component
public class AnnotatedBean{
@Autowired
private ICarpoolDoa carpoolDAO;
@Condition
public boolean condition(CustomLocation customLocation, String userId) {
//snip
}
@Action
public void action() {
//snip
}
Run Code Online (Sandbox Code Playgroud)
我的自定义注释之一的示例
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Condition {
}
Run Code Online (Sandbox Code Playgroud)
findAndDoStuffWithAnnotatedThings Bean中的错误
被传递到验证我的自定义注释的类,但我的验证程序找不到任何注释。(Util 使用isAnnotationPresent方法)。同样,当我使用“new”自己创建 bean 时,没有问题。
public class RuleAnnotationVerifier {
public void RuleAnnotationVerifier(final Object rule) {
checkRuleClass(rule);
checkConditionMethod(rule);
checkActionMethod(rule);
}
private void checkRuleClass(final Object rule) {
if …
Run Code Online (Sandbox Code Playgroud) 我有一个 spring bean,创建后需要更改它,但我无权编辑 bean 类的源代码;所以我不能用...
有什么方法可以检测到这个 bean 是由另一个类中的 id 创建的,然后应用我需要的更改吗?
谢谢
我正在制作 Java REST 应用程序。我想知道我应该如何实现我的服务 - 我应该为整个应用程序使用静态服务变量还是像 Spring MVC 中那样将服务作为单例。单例对象和在应用过程中只初始化一次对象有什么区别吗?
我定义了一个数据源如下:
@Bean
public DataSource dataSource(){
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.h2.Driver");
dataSource.setUrl("jdbc:h2:tcp://localhost/~/test");
dataSource.setUsername("sa");
dataSource.setPassword("");
return dataSource();
}
Run Code Online (Sandbox Code Playgroud)
在 spring 的引导过程中,控制台抛出了一个巨大的:
Dez 07, 2016 5:00:53 PM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
INFO: Loaded JDBC driver: org.h2.Driver
Run Code Online (Sandbox Code Playgroud)
过了一会儿我得到
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class sample.config.AppConfig: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource sample.config.AppConfig.dataSource()] threw exception; nested exception is java.lang.StackOverflowError
Run Code Online (Sandbox Code Playgroud)
我从某本书中复制的所有示例,这里有什么问题?
问题可能是,我已经把它放到了 web 应用程序配置类中吗?
我将此添加到WAR文件中的Spring配置中:
<import resource="classpath:myapp-custom-settings.xml"/>
Run Code Online (Sandbox Code Playgroud)
这样,用户可以创建一个myapp-custom-settings.xml
文件(在shared.loader中列出的文件夹中)来修改他们想要的bean而不必解压缩WAR.
非常方便,但大多数用户没有这样的文件,在这种情况下会发生FileNotFoundException并且应用程序无法启动:
ERROR: org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.parsing.BeanDefinitionParsingException:Configuration problem: Failed to import bean definitions from URL location [file:/home/nico/tomcat/conf/myapp-custom-settings.xml]
Offending resource: ServletContext resource [/WEB-INF/spring/root-context.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from URL [file:/home/nico/tomcat/conf/myapp-custom-settings.xml]; nested exception is
java.io.FileNotFoundException: /home/nico/tomcat/conf/myapp-custom-settings.xml (No such file or directory)
at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:70)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:76)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.importBeanDefinitionResource(DefaultBeanDefinitionDocumentReader.java:233)
Run Code Online (Sandbox Code Playgroud)
问题:有没有办法使这个导入失败?
我的意思是,加载文件(如果存在),但如果文件不存在则跳过.
我正在尝试将 Datasource 实例注册为 java 代码中的 bean(spring-boot 项目)
这是我写的。(此代码不起作用。)
@Configuration
public class DatabaseConfig {
private Logger logger = Logger.getLogger(DatabaseConfig.class);
@Autowired
ApplicationContext context;
private Map<String, Map<String, String>> dsMap;
private Map<String, String> getTestDataSourceInfo () {
Map<String, String> ds = new HashMap<String, String> ();
ds.put("driverClassName", "com.mysql.jdbc.Driver");
ds.put("url", "jdbc:mysql://123.456.78.912:3306/test");
ds.put("username", "testuser");
ds.put("password", "testuser");
return ds;
}
public DatabaseConfig () {
this.dsMap = new HashMap<String, Map<String, String>>();
dsMap.put("sampleDs", getTestDataSourceInfo());
}
@PostConstruct
public void loadDataSource () {
logger.info("DS ================================ :: " + String.valueOf(this.dsMap));
this.dsMap.forEach((k,v) -> {
logger.info("value …
Run Code Online (Sandbox Code Playgroud) 我想自动装配一个服务于我的请求过滤器。它给了我一个空指针异常,我不太确定为什么。我在我的控制器中使用了完全相同的 autowire 类,它按预期工作!
@Component(value="jwtFilter")
public class JwtFilter extends GenericFilterBean {
RedisClient redisClient = new RedisClient();
@Autowired
JwtTokenService jwtServ; // Is null for some reason??!!
@Override
public void doFilter(final ServletRequest req,
final ServletResponse res,
final FilterChain chain) throws IOException, ServletException {
// SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this); // Doesnt fix issue
System.out.println("\nMaking request...");
final HttpServletRequest request = (HttpServletRequest) req;
final String authHeader = request.getHeader("Authorization");
if (authHeader == null || !authHeader.startsWith("Bearer ")) {
throw new ServletException("Missing or invalid Authorization header."); …
Run Code Online (Sandbox Code Playgroud) spring-bean ×8
spring ×5
java ×4
spring-boot ×2
annotations ×1
datasource ×1
file ×1
grails ×1
import ×1
spring-mvc ×1
web-services ×1