我的代码:
@Component
public class A {
@Autowired
private B b;
public void method() {}
}
public interface X {...}
@Component
public class B implements X {
...
}
Run Code Online (Sandbox Code Playgroud)
我想在隔离类A中测试.我必须模拟B类吗?如果有,怎么样?因为它是自动装配的,并且没有可以发送模拟对象的setter.
我正在尝试使用Spring为webapp自动装配一些bean(用于依赖注入).一个控制器bean包含另一个bean,而另一个bean又拥有另一组bean的hashmap.目前,地图只有一个条目.当我在tomcat中运行并调用该服务时,我得到一个错误,说第二个bean(保存在控制器中)不是唯一的
No unique bean of type [com.hp.it.km.search.web.suggestion.SuggestionService] is defined: expected single matching bean but found 2: [suggestionService, SuggestionService]
Run Code Online (Sandbox Code Playgroud)
我无法看到我在两次定义bean的位置,但是我是Spring的新手并且自动装配,所以我可能会遗漏一些基本的东西.下面列出的xml和2类的源代码......
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.hp.it.km.search.web.suggestion" />
<mvc:annotation-driven />
<context:annotation-config />
<bean id="SuggestionController" class="com.hp.it.km.search.web.suggestion.SuggestionController">
<property name="service">
<ref bean="SuggestionService" />
</property>
</bean>
<bean id="SuggestionService" class="com.hp.it.km.search.web.suggestion.SuggestionService">
<property name="indexSearchers">
<map>
<entry key="KMSearcher"> <ref bean="KMSearcherBean"></ref></entry>
</map>
</property>
</bean>
<bean id="KMSearcherBean" class="com.hp.it.km.search.web.suggestion.SuggestionIndexSearcher">
<constructor-arg index="0" value="KMSearcher" />
<constructor-arg index="1" value="C://dev//workspace//search-restful-webapp//src//main//resources//indexes//keyword" />
</bean>
Run Code Online (Sandbox Code Playgroud)
带有自动控制器和服务bean的类asscoaites在这里......
@Controller
public class …Run Code Online (Sandbox Code Playgroud) spring annotations dependency-injection spring-mvc autowired
我有这个Spring配置:
<bean id="boo" class="com.x.TheClass"/>
Run Code Online (Sandbox Code Playgroud)
该类TheClass实现TheInterface.然后我有这个(假设的)Java代码:
@Autowired
TheInterface x;
@Autowired
TheClass y;
Run Code Online (Sandbox Code Playgroud)
自动装配TheInterface工程但自动装配TheClass失败.春天给了我一NoSuchBeanDefinitionException堂课.
为什么你可以连接界面而不是课程?
我@Autowired在@Configuration类构造函数下使用注释.
@Configuration
public class MyConfiguration {
private MyServiceA myServiceA;
private MyServiceB myServiceB
@Autowired
public MyConfiguration(MyServiceA myServiceA, MyServiceB myServiceB){
this.myServiceA = myServiceA;
this.myServiceB = myServiceB;
}
}
Run Code Online (Sandbox Code Playgroud)
作为Spring文档sais,我能够声明是否需要带注释的依赖项.
如果我@Autowired在构造函数下标记注释required=false,我是说要自动装配的两个服务不是必需的(如Spring文档所示):
@Autowired(required = false)
public MyConfiguration(MyServiceA myServiceA, MyServiceB myServiceB){
this.myServiceA = myServiceA;
this.myServiceB = myServiceB;
}
Run Code Online (Sandbox Code Playgroud)
从Spring文档:
在多参数方法的情况下,'required'参数适用于所有参数.
如何required单独为每个构造函数参数设置属性?是否有必要@Autowired在每个领域下使用注释?
问候,
我将环境连接到Spring项目时遇到问题.在这堂课上
@Configuration
@ComponentScan(basePackages = "my.pack.offer.*")
@PropertySource("classpath:OfferService.properties")
public class PropertiesUtil {
@Autowired
private Environment environment;
@Bean
public String load(String propertyName)
{
return environment.getRequiredProperty(propertyName);
}
}
Run Code Online (Sandbox Code Playgroud)
环境始终为null.
在我的Spring Boot应用程序中,假设我有Java接口:
public interface MyFilter<E extends SomeDataInterface>
Run Code Online (Sandbox Code Playgroud)
(一个很好的例子是Spring的公共接口ApplicationListener <E extends ApplicationEvent>)
我有几个实现,如:
@Component
public class DesignatedFilter1 implements MyFilter<SpecificDataInterface>{...}
@Component
public class DesignatedFilter2 implements MyFilter<SpecificDataInterface>{...}
@Component
public class DesignatedFilter3 implements MyFilter<AnotherSpecificDataInterface>{...}
Run Code Online (Sandbox Code Playgroud)
然后,在某些对象中,我有兴趣利用所有实现MyFilter <SpecificDataInterface> 但不是 MyFilter <AnotherSpecificDataInterface>的过滤器
这会是什么语法?
我想在servlet中使用spring autowiring,所以这是我的代码:
@Configurable
public class ImageServlet extends HttpServlet {
@Autowired
private SystemPropertyDao systemPropertyDao;
@Override
public void init() throws ServletException {
String imagePath = systemPropertyDao.findByID(StaticParam.CONTENT_FOLDER);
}
Run Code Online (Sandbox Code Playgroud)
而SystemPropertyDao注释时@Repository
和我的applicationContext.xml:
<context:component-scan base-package="com.basepackage" />
<mvc:annotation-driven />
<context:annotation-config />
<context:spring-configured/>
Run Code Online (Sandbox Code Playgroud)
web.xml:
<servlet>
<servlet-name>imageServlet</servlet-name>
<servlet-class>com.xeno.basepackage.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>imageServlet</servlet-name>
<url-pattern>/myimages/*</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
有时自动装配工作,有时它不工作(对spring bean systemPropertyDao的引用为null),任何人都可以告诉我,如果我错过了什么?
该FactoryBean的可用于以编程方式创建,这可能需要复杂的实例化逻辑的对象.
但是,似乎由它创建的bean FactoryBean不会成为弹簧管理.这种解释是否正确?如果是这样,有什么好的解决方法吗?包含一个简短的代码示例来说明我的问题.
ApplicationContext的:
<bean id="searcher" class="some.package.SearcherFactory" />
<bean id="service" class="some.package.Service" />
Run Code Online (Sandbox Code Playgroud)
工厂实施:
public class SearcherFactory implements FactoryBean<Searcher> {
@Override
public Searcher getObject() throws Exception {
return new Searcher(); // not so complex after all ;)
}
@Override
public Class<Searcher> getObjectType() {
return Searcher.class;
}
....
}
Run Code Online (Sandbox Code Playgroud)
工厂创建的类:
public class Searcher() {
private Service service;
@Autowired
public void setService(Service service) {
// never invoked
this.service=service;
}
}
Run Code Online (Sandbox Code Playgroud) 哪个更正确?
这个(方法上有@Autowired注释)?
@Controller
public class MyController
{
private MyDao myDao;
@Autowired
public MyController(MyDao myDao)
{
this.myDao = myDao;
}
Run Code Online (Sandbox Code Playgroud)
这个(在属性上有@Autowired注释)?
@Controller
public class MyController
{
@Autowired
private MyDao myDao;
public MyController(MyDao myDao)
{
this.myDao = myDao;
}
Run Code Online (Sandbox Code Playgroud)
@Autowired注释应该去哪里?
spring annotations dependency-injection spring-mvc autowired
是否可以使用@Autowired列表?
就像我有mimetypes属性文件一样,在我的类文件中我有这样的东西
@Autowired
private List<String> mimeTypes = new ArrayList<String>();
Run Code Online (Sandbox Code Playgroud) autowired ×10
spring ×10
java ×4
annotations ×2
spring-boot ×2
spring-mvc ×2
constructor ×1
environment ×1
java-ee ×1
junit ×1
list ×1
properties ×1
servlets ×1