我需要将外部lib类连接到我的bean,以便将其用作单例.
.xml配置:
<bean id="myBean" class="com.my.MyBean">
<property name="someLib" value="com.ExternalBean" />
</bean>
Run Code Online (Sandbox Code Playgroud)
java bean:
@Service
public class MyBean {
@Autowired
private ExternalBean externalBean;
public void setExternalBean(ExternalBean externalBean) {
this.externalBean = externalBean;
}
Run Code Online (Sandbox Code Playgroud)
此外,我externalBean在公共方法中使用有线变量,以便不在每个方法调用中实例化它.问题是它null.
我是否正确连接豆?什么是错误.
我试图控制Spring的自动装配,但似乎无法正确地实例化bean(a DocumentBuilder)。我已经这样创建了一个自定义JSP标记:
public class MyTag extends SimpleTagSupport {
@Autowired
private DocumentBuilder documentBuilder;
public void setBuilder(DocumentBuilder builder) {
this.documentBuilder = builder;
}
@Override
public void doTag() throws IOException {
// documentBuilder is null in here!
}
}
Run Code Online (Sandbox Code Playgroud)
这是servlet配置:
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
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">
<!-- Scan for HTTP/REST controllers -->
<context:component-scan base-package="the.right.package" />
<context:annotation-config/>
<bean id="documentBuilderFactory"
class="javax.xml.parsers.DocumentBuilderFactory"
factory-method="newInstance">
<property name="validating" value="false" />
<property name="ignoringElementContentWhitespace" value="true" />
</bean>
<bean id="documentBuilder" class="javax.xml.parsers.DocumentBuilder"
factory-bean="documentBuilderFactory" …Run Code Online (Sandbox Code Playgroud) 尝试在我的solrQueue中添加任何内容时,我得到空指针异常.我检查了调试器,这是因为solrQueue为null.但是我已经在我的应用程序上下文中自动启动了它为什么会出现此错误?
public class Check {
@Autowired
public LinkedBlockingQueue<SolrInputDocument> solrQueue;
public SolrInputDocument solrDoc;
public void solradd(){
solrDoc=new SolrInputDocument();
solrDoc.addField("title", "abc");
solrQueue.add(solrDoc);//solrQueue is null
}
}
Run Code Online (Sandbox Code Playgroud)
应用Context.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config />
<!--<context:component-scan base-package="com/abc" /> -->
<bean id="solrQueue" class="java.util.concurrent.LinkedBlockingQueue" />
<bean id="check" class="com.abc.Check" scope="prototype" />
</beans>
Run Code Online (Sandbox Code Playgroud) 我正在编写一个网站的功能区/成就系统,我必须为我的系统中的每个功能区编写一些逻辑.例如,如果您是第一批注册到该网站的2,000人或者在论坛中发布了1,000个帖子后,您就可以获得一个功能区.这个想法非常类似于stackoverflow的徽章,真的.
因此,每个功能区显然都在数据库中,但它们还需要一些逻辑来确定用户何时获得功能区.
在我编写它的方式,Ribbon是一个简单的界面:
public interface Ribbon {
public void setId(int id);
public int getId();
public String getTitle();
public void setTitle(String title);
public boolean isEarned(User user);
}
Run Code Online (Sandbox Code Playgroud)
RibbonJpa是一个实现Ribbon接口的抽象类,避免了isEarned()方法的定义:
@Entity
@Table(name = "ribbon")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "ribbon_type")
public abstract class RibbonJpa implements Ribbon {
@Id
@Column(name = "id", nullable = false)
int id;
@Column(name = "title", nullable = false)
private String title;
@Override
public int getId() {
return id;
}
@Override
public void …Run Code Online (Sandbox Code Playgroud) 我无法运行附加的Spring Boot采样器应用程序.它有一个AMQP启动器,需要RabbitMQ.从根本上说,它是一个简单的应用程序,只是向RabbitMQ Exchange发送一个消息,并绑定一个队列.我收到以下错误:
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.company.messaging.MessageDeliveryManager com.company.exec.Application.messageDeliveryManager; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.company.messaging.MessageDeliveryManager] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations:
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
Run Code Online (Sandbox Code Playgroud)
Application.java
package com.company.exec;
@SpringBootApplication
public class Application implements CommandLineRunner {
@Autowired
MessageDeliveryManager messageDeliveryManager;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
public void run(String... args) throws Exception {
messageDeliveryManager.sendMessage(String message);
}
}
Run Code Online (Sandbox Code Playgroud)
MessageDeliveryManager.java
package com.company.messaging;
public …Run Code Online (Sandbox Code Playgroud) 我正在处理的应用程序中使用注入有一些问题(使用Spring Version 3.1.2).首先,我看到很多这样的代码:
@Value("#{searchRequestBean}")
private SearchRequest searchRequest;
@Value("#{searchResponseBean}")
private SearchResponse searchResponse;
@Autowired
private SavedSearchService service;
Run Code Online (Sandbox Code Playgroud)
这三个中的每一个似乎都具有将指定的bean/service自动装配到类中的效果.我不明白的是,有什么之间的区别@Value,并@Autowired在这些情况下?我在网上找到的每个例子似乎都用于@Value从属性文件中注入值.在这种情况下,SearchResponse并且SearchRequest是抽象类.
我希望更好地理解这一点将有助于我解决我对Session bean的一些问题.
java spring dependency-injection autowired spring-annotations
我在Spring中实现了一个工厂bean来实例化超类的不同子类.我遇到的问题是超类属性不存在@Autowired(我猜是由于new工厂方法中的命令).这是我的代码:
@Component
public class ConfigBeanImpl implements ConfigBean{
@Override
public String expandParam(String param) {
return String.format("expanded %s", param);
}
}
public abstract class FactoryBean {
@Autowired
protected ConfigBean configBean;
private String property;
protected FactoryBean() {
this.property = configBean.expandParam("property");
}
public abstract String getProperty();
public static FactoryBean GET(int id) {
return new FactoryBeanGet(id);
}
public static FactoryBean POST(String param){
return new FactoryBeanPost(param);
}
}
public class FactoryBeanGet extends FactoryBean {
private int id;
protected FactoryBeanGet(int id) {
this.id …Run Code Online (Sandbox Code Playgroud) 我是春季和春季靴子的新手,所以希望这不是一个愚蠢的问题.
我有几个实现的接口.实现注释为@Component("NameOfImpl").
我的目标是使用选定的实现自动装配bean.在正常情况下我可以使用@Autowired @Qualifier("NameOfImpl"),但我的问题是我想在一个方法中选择一个实现,如:
public void doSomethingMethod(){
for(String line: configFile){
String[] values = line.split(";");
if (values[0].equals("A")) {
//here I want to select an bean implementation
}
else if (values[0].equals("B")) {
//here I want to select another bean implementation
}
}
bean.doSomething();
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?你有什么建议?谢谢!
我在使用Spring框架的Java应用程序中遇到以下问题。
所以我有以下情况,进入root-context.xml配置文件,我有了这个bean配置:
<!-- Definition for datiPianiInterventiDaoImpl bean -->
<bean id="datiPianiInterventiDaoImpl" class="it.myCompany.myclient.batch.dao.DatiPianiInterventiDaoImpl">
<property name="dataSource" ref="dataSource" />
</bean>
Run Code Online (Sandbox Code Playgroud)
好的,它可以正常工作,并且此bean已正确创建并正常工作。
问题是,现在在这个bean中,我还必须注入org.springframework.core.env.Environment Spring类的实例。
所以我尝试用这种方式做:
public class DatiPianiInterventiDaoImpl implements DatiPianiInterventiDao {
@Autowired
private Environment env;
...................................................
...................................................
...................................................
}
Run Code Online (Sandbox Code Playgroud)
但这似乎不起作用,因为当我执行应用程序时,Environment env的值为 null。
的 @Autowired被激活,因为我使用此批注在其他班级啊,我的项目。
那么可能是什么问题呢?我想,也许它可以通过这样的事实依赖,我定义我有豆ID =“datiPianiInteventiDaoImpl”到我 root-context.xml中(在这里我还定义了注入该bean的依赖项)。
所以也许我不能将XML依赖注入与 @Autowired?
怎么了?我想念什么?如何将Environment实例正确地注入此类?
spring dependency-injection spring-mvc autowired xml-configuration
我有一个小问题。这可能是微不足道的,但我以前从未遇到过。
我有通用接口及其通用实现。我想为其自动接线,但是发生了错误。详细信息如下:
接口
@Service
public interface Serializing<T extends Serializable> {
String serialize(T toBeSerialized);
T deserialize(String toBeDeserialized, Class<T> resultType);
}
Run Code Online (Sandbox Code Playgroud)
实作
@Service
public class JsonSerializer<T extends Serializable> implements Serializing<T> {
/** code **/
}
Run Code Online (Sandbox Code Playgroud)
自动接线尝试
private NoteDAO noteDAO;
@Qualifier("jsonSerializer")
private Serializing<UserComment> serializer;
@Autowired
public NoteController(NoteDAO noteDAO, Serializing<UserComment> serializer) {
this.noteDAO = noteDAO;
this.serializer = serializer;
}
Run Code Online (Sandbox Code Playgroud)
错误
Parameter 1 of constructor in somepackagepath.NoteController required a bean of type 'anotherpackagepath.Serializing' that could not be found.
Run Code Online (Sandbox Code Playgroud)
我想让它尽可能简单。我已经检查过Web,但是只发现了有关在配置中定义确切的bean的信息。如果可能,我宁愿避免这样做。
autowired ×10
spring ×10
java ×6
spring-boot ×2
spring-mvc ×2
factory ×1
generics ×1
inheritance ×1
javabeans ×1
jpa-2.0 ×1
rabbitmq ×1