我正在使用Spring 3.0.5并尽可能地为我的班级成员使用@Autowire注释.我需要自动装配的bean之一需要其构造函数的参数.我查看了Spring文档,但似乎找不到任何关于如何注释构造函数参数的引用.
在XML中,我可以将其用作bean定义的一部分.@Autowire注释是否有类似的机制?
例如:
@Component
public class MyConstructorClass{
String var;
public MyConstructorClass( String constrArg ){
this.var = var;
}
...
}
@Service
public class MyBeanService{
@Autowired
MyConstructorClass myConstructorClass;
....
}
Run Code Online (Sandbox Code Playgroud)
在此示例中,如何使用@Autowire注释在MyBeanService中指定"constrArg"的值?有没有办法做到这一点?
谢谢,
埃里克
我在Spring bean中有以下内容:
@Value("${myValue}")
private String value;
Run Code Online (Sandbox Code Playgroud)
正确注入该值.但是,变量必须是可选的,它作为命令行参数传递(然后使用SimpleCommandLinePropertySource将其添加到Spring上下文中),并且此参数不会始终存在.
为了提供默认值,我尝试了以下两种方法:
@Value("${myValue:}")
@Value("${myValue:DEFAULT}")
Run Code Online (Sandbox Code Playgroud)
但是在每种情况下,冒号后的默认参数即使存在实际值也会被注入 - 这会覆盖Spring应该注入的内容.
指定不需要@Value的正确方法是什么?
谢谢
我正在使用带有注释的Spring Beans,我需要在运行时选择不同的实现.
@Service
public class MyService {
public void test(){...}
}
Run Code Online (Sandbox Code Playgroud)
例如对于我需要的Windows平台MyServiceWin extending MyService
,对于我需要的linux平台MyServiceLnx extending MyService
.
现在我只知道一个可怕的解决方案:
@Service
public class MyService {
private MyService impl;
@PostInit
public void init(){
if(windows) impl=new MyServiceWin();
else impl=new MyServiceLnx();
}
public void test(){
impl.test();
}
}
Run Code Online (Sandbox Code Playgroud)
请考虑我只使用注释而不是XML配置.
我是Spring MVC的新手,对Java中Java bean的使用有一点了解.
Java bean和Spring bean之间的基本区别是什么?
我正在尝试使用Spring Boot在本地设置DynamoDB.最初我使设置工作,并能够通过存储库写入/保存到DynamoDB.从那时起,我添加了更多类来构建我的应用程序.现在当我尝试启动我的应用程序时,我得到以下异常:
org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'agentRepository' defined in null: Cannot register bean definition [Root bean: class [org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBRepositoryFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] for bean 'agentRepository': There is already [Root bean: class [org.socialsignin.spring.data.dynamodb.repository.support.DynamoDBRepositoryFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] bound.
我已经广泛搜索了SO和互联网,但没有任何有用的解决方案.错误消息也具有误导性.
我的项目具有以下层次结构
ai.test.as
- as
- agent
- business
- intent
- exception
- Agent.java
- AgentDTO.java
- AgentRespository.java
- AgentController.java
- AgentService.java
- AgentServiceImpl.java
- config …
Run Code Online (Sandbox Code Playgroud) 我想在Spring Java配置中创建一个Spring bean,并在运行时传递一些构造函数参数.我创建了以下Java配置,其中有一个bean fixedLengthReport,它需要构造函数中的一些参数.
@Configuration
public class AppConfig {
@Autowrire
Dao dao;
@Bean
@Scope(value = "prototype")
**//SourceSystem can change at runtime**
public FixedLengthReport fixedLengthReport(String sourceSystem) {
return new TdctFixedLengthReport(sourceSystem, dao);
}
}
Run Code Online (Sandbox Code Playgroud)
但我收到的错误是sourceSystem无法连接,因为没有找到bean.如何使用运行时构造函数参数创建bean?
我使用的是Spring 4.2
我对@PostConstruct
Spring中的自动布线顺序和逻辑有疑问.例如,下面的演示代码我有一个主要的Spring Boot类:
@SpringBootApplication
public class Demo1Application {
@Autowired
BeanB beanb;
public static void main(String[] args) {
SpringApplication.run(Demo1Application.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
和2 @Service
定义:
@Service
public class BeanB {
@Autowired
private BeanA beana ;
@PostConstruct
public void init(){
System.out.println("beanb is called");
}
public void printMe(){
System.out.println("print me is called in Bean B");
}
}
@Service
public class BeanA {
@Autowired
private BeanB b;
@PostConstruct
public void init(){
System.out.println("bean a is called");
b.printMe();
}
}
Run Code Online (Sandbox Code Playgroud)
我有以下输出:
豆a被称为
打印我在Bean B中调用
beanb被称为 …
我有这样的豆:
@Bean
public String myBean(){
return "My bean";
}
Run Code Online (Sandbox Code Playgroud)
我想要自动装配它:
@Autowired
@Qualifier("myBean")
public void setMyBean(String myBean){
this.myBean=myBean;
}
Run Code Online (Sandbox Code Playgroud)
我需要这样的东西:
@Bean(name="myCustomBean")
Run Code Online (Sandbox Code Playgroud)
是否可以使用开箱即用的bean自定义名称?如果不可能开箱即可,那么如何创建这样的bean呢?
我注意到@PreDestroy
我的原型范围Spring bean 的钩子没有被执行.
我在这里读到这实际上是设计的.Spring容器将销毁单例bean,但不会销毁原型bean.我不清楚为什么.如果Spring容器将创建我的原型bean并执行它的@PostConstruct
钩子,为什么在容器关闭时它也不会破坏我的bean?一旦我的Spring容器关闭,继续使用它的任何bean都有意义吗?我看不到你想要在完成bean之前关闭容器的场景.甚至可以在容器关闭后继续使用原型Spring bean吗?
上面描述了我的主要问题的令人费解的背景:如果Spring容器没有破坏原型bean,那是否意味着可能发生内存泄漏?或者原型bean会在某些时候被垃圾收集?
春季文件指出:
客户端代码必须清理原型范围的对象并释放原型bean所持有的昂贵资源.要使Spring容器释放原型范围的bean所拥有的资源,请尝试使用自定义bean后处理器,它包含对需要清理的bean的引用.
那是什么意思?该文本告诉我,我作为程序员负责明确(手动)销毁我的原型bean.它是否正确?如果是这样,我该怎么做?
我想我对春豆的理解有点过时了.
我正在研究我的项目,我正在考虑这种情况.
说我上课了 Foo
class Foo(){
public void doSomething(Object a , Object b){ // input parameters does not matter actually.
//do something
}
}
Run Code Online (Sandbox Code Playgroud)
如果我在另一个类中使用此类,如:
class Scheduler{
....
@Autowired
private Foo foo;
someMethod(){
foo.doSomeThind(a,b);
}
....
}
Run Code Online (Sandbox Code Playgroud)
在上面的情况下Foo
,我可以使doSomeThing
静态和直接使用而不是自动装配Foo.doSomeThing(a,b)
我只是想知道创建一个bean是否有任何优势,或者是否有使用这样的静态方法的任何缺点?
如果它们是相同的,我什么时候应该使用spring bean?何时应该使用静态方法?
spring-bean ×10
spring ×9
java ×8
spring-mvc ×2
autowired ×1
constructor ×1
javabeans ×1
predestroy ×1
spring-boot ×1