在了解了Spring @Autowired用法的问题之后,我想为另一个弹簧布线选项(@Configuration该类)创建一个完整的知识库.
假设我有一个如下所示的spring XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<import resource="another-application-context.xml"/>
<bean id="someBean" class="stack.overflow.spring.configuration.SomeClassImpl">
<constructor-arg value="${some.interesting.property}" />
</bean>
<bean id="anotherBean" class="stack.overflow.spring.configuration.AnotherClassImpl">
<constructor-arg ref="someBean"/>
<constructor-arg ref="beanFromSomewhereElse"/>
</bean>
</beans>
Run Code Online (Sandbox Code Playgroud)
我该怎么用@Configuration呢?它对代码本身有什么影响吗?
我正在使用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 3.1.4.RELEASE和Mockito 1.9.5.在我的Spring课程中,我有:
@Value("#{myProps['default.url']}")
private String defaultUrl;
@Value("#{myProps['default.password']}")
private String defaultrPassword;
// ...
Run Code Online (Sandbox Code Playgroud)
从我目前设置的JUnit测试开始,如下所示:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:test-context.xml" })
public class MyTest
{
Run Code Online (Sandbox Code Playgroud)
我想为我的"defaultUrl"字段模拟一个值.请注意,我不想模拟其他字段的值 - 我想保留它们的原样,只保留"defaultUrl"字段.另请注意,setDefaultUrl我的课程中没有明确的"setter"方法(例如),我不想仅仅为了测试而创建任何方法.
鉴于此,我如何模拟该字段的值?
我在一些帖子中读过,Spring MVC并Portlets建议不要进行现场注射.因为我试图得到一个所以我问自己我是否使用现场注射,我无法回答它.据我所知场注入是,如果你注入一个Bean与一个属性@Autowired是这样的:
CartController.java:
...
@Autowired
private Cart cart;
...
Run Code Online (Sandbox Code Playgroud)
BookshopConfiguartion.java:
@Configuration
public class BookShopConfiguration {
@Bean
public Cart cart(){
return new Cart();
}
//more configuration
Run Code Online (Sandbox Code Playgroud)
我Cart.java习惯于在购物车中存储和提供有关书籍的信息.
在我的研究期间,我读到了构造函数注入:
MyComponent.java:
...
public class MyComponent{
private Cart cart;
@Autowired
public MyComponent(Cart cart){
this.cart = cart;
}
...
Run Code Online (Sandbox Code Playgroud)
这两种注射的优点和缺点是什么?
编辑1:由于此问题被标记为此问题的重复,我检查了它.因为在问题和答案中都没有任何代码示例,我不清楚我是否正确猜测我正在使用哪种注射类型.
下面显示的是一段代码,我尝试引用我的ApplicationProperties bean.当我从构造函数引用它时它是null,但是当从另一个方法引用它时它很好.到目前为止,我在其他类中使用这个自动装配的bean没有任何问题.但这是我第一次尝试在另一个类的构造函数中使用它.
在下面的代码片段中,当从构造函数调用时,applicationProperties为null,但在convert方法中引用时则不是.我错过了什么
@Component
public class DocumentManager implements IDocumentManager {
private Log logger = LogFactory.getLog(this.getClass());
private OfficeManager officeManager = null;
private ConverterService converterService = null;
@Autowired
private IApplicationProperties applicationProperties;
// If I try and use the Autowired applicationProperties bean in the constructor
// it is null ?
public DocumentManager() {
startOOServer();
}
private void startOOServer() {
if (applicationProperties != null) {
if (applicationProperties.getStartOOServer()) {
try {
if (this.officeManager == null) {
this.officeManager = new DefaultOfficeManagerConfiguration()
.buildOfficeManager();
this.officeManager.start();
this.converterService = new …Run Code Online (Sandbox Code Playgroud) 我有必须@Autowired在静态方法中使用的服务.我知道这是错的,但我不能改变当前的设计,因为它需要大量的工作,所以我需要一些简单的黑客.我不能改为randomMethod()非静态,我需要使用这个自动装配的bean.任何线索怎么做?
@Service
public class Foo {
public int doStuff() {
return 1;
}
}
public class Boo {
@Autowired
Foo foo;
public static void randomMethod() {
foo.doStuff();
}
}
Run Code Online (Sandbox Code Playgroud) 我通过使用Spring和Hibernate为服务创建实体,服务和JUnit测试来启动我的项目.所有这一切都很棒.然后我添加了spring-mvc来使用许多不同的分步教程来创建这个Web应用程序,但是当我尝试使用@Autowired注释创建Controller时,我在部署期间遇到来自Glassfish的错误.我想由于某些原因Spring没有看到我的服务,但经过多次尝试后我仍然无法处理它.
服务测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/beans.xml"})
Run Code Online (Sandbox Code Playgroud)
和
@Autowired
MailManager mailManager;
Run Code Online (Sandbox Code Playgroud)
工作正常.
没有@Autowired的控制器,我可以毫无困难地在Web浏览器中打开我的项目.
/src/main/resources/beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd">
<context:property-placeholder location="jdbc.properties" />
<context:component-scan base-package="pl.com.radzikowski.webmail">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!--<context:component-scan base-package="pl.com.radzikowski.webmail.service" />-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- Persistance Unit Manager for persistance options managing …Run Code Online (Sandbox Code Playgroud) 有没有一种简单的方法可以在Spring 3.1中从自动装配中排除包/子包?
例如,如果我想在基础包中包含组件扫描,com.example是否有一种简单的排除方法com.example.ignore?
(为什么?我想从集成测试中排除一些组件)
这是我的代码:
public class Main {
public static void main(String[] args) {
Main p = new Main();
p.start(args);
}
@Autowired
private MyBean myBean;
private void start(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("META-INF/config.xml");
System.out.println("my beans method: " + myBean.getStr());
}
}
@Service
public class MyBean {
public String getStr() {
return "string";
}
}
Run Code Online (Sandbox Code Playgroud)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="mypackage"/>
</beans>
Run Code Online (Sandbox Code Playgroud)
为什么这不起作用?我得到NullPointerException.是否可以在独立应用程序中使用自动装配?
java spring program-entry-point dependency-injection autowired
我有一个bean Item<T>需要在@Configuration课堂上自动装配.
@Configuration
public class AppConfig {
@Bean
public Item<String> stringItem() {
return new StringItem();
}
@Bean
public Item<Integer> integerItem() {
return new IntegerItem();
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试时@Autowire Item<String>,我得到以下异常.
"No qualifying bean of type [Item] is defined: expected single matching bean but found 2: stringItem, integerItem"
Run Code Online (Sandbox Code Playgroud)
我应该如何Item<T>在Spring中自动加载通用类型?
autowired ×10
spring ×9
java ×7
spring-mvc ×2
annotations ×1
constructor ×1
generics ×1
mockito ×1
portlet ×1
spring-bean ×1
static ×1