Tag: autowired

Spring构造函数dependency injection – 有和没有自动assembly

我遇到了这个问题中描述的问题 ,我已经通过将@Autowired注释添加到构造函数来解决了这个问题 。 现在我想知道,为什么它有所帮助。 有什么区别 public RegistrationController(UserDao userDao) { this.userDao = userDao; } 和 @Autowired public RegistrationController(UserDao userDao) { this.userDao = userDao; } 在这两种情况下,userDao都会注入Controller。 我发现的唯一区别是,使用@persistenceContext注释标记的entityManager仅在第二个示例中注入userDao。 但我不知道为什么。 有什么线索吗? 还有其他差异吗? 编辑:我的servlet上下文如下所示: <!– Set loading annotations from classes –> <!—-> 编辑2:控制器: package com.fido.pia; import com.fido.pia.dao.UserDao; import com.fido.pia.model.User; import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import […]

自动assemblySpring 3.2独立应用程序失败

这是我第一次在stackoverflow上找不到解决问题的方法: 我正在尝试创建一个带有基于注释的自动assembly的可执行jar独立应用程序,但我无法获得可运行的jar文件(从Eclipse导出为可运行的JAR文件)来完成它的工作。 它确实直接从Eclipse运行为Java应用程序,而不是通过控制台> java -jar test.jar作为独立应用程序运行。 Exception in thread “main” java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58) Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘handler’: Injection of autowired dependen cies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.example.serv ice.UserService com.example.controller.TestHandler.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionEx ception: No […]

Spring MVC何时会自动生成HttpSession?

使用AutoWired HttpSession的问题: LoginController调用LoginService传递HttpServletRequest作为参数。 我已经在其他几个带注释的类中自动assembly了HttpSession(但不是在LoginService中): @Autowired private HttpSession httpSession; 在LoginService类中,如果我尝试通过调用request.getSession(false)来获取会话,则在某些情况下我会收到null。 如果我尝试通过调用request.getSession(true)来获取会话,我最终会得到两个HttpSession对象(一个在这里,另一个通过AutoWiring)。 如果我在LoginServic类中自动assemblyHttpSession并从那里使用会话,那么我也将以两个HttpSession对象结束。 何时会创建完全自动assembly的HttpSession? 处理这种情况的最佳方法是什么? 谢谢!

弹簧和multithreading

我需要启动一个可变数量的线程,这些线程又会在spring应用程序中启动不同数量的线程(即I线程需要启动Ki线程的线程)。 假设每个“I线程”包含一个自动assembly的内部类,我将如何生成这些实例? 所以我有一个A bean需要以某种方式生成I需要进行Spring管理以满足其依赖关系的bean实例。 我写了一个简短的示例代码,我认为是我的解决方案的基础,我已经标记了代码,我不知道如何编写???: @Component public class MasterOrchestrator { public void do(List list){ ExecutorService es = Executors.newFixedThreadPool(list.size()); for (DataObjWrapper dataObjWrapper : list){ es.submit(???); } } } @Component public class ThreadWorkerI implements Runnable{ private int numThreadsForMessageType; private int numRunsForMessageType; private DataObj dataObj; public ThreadWorkerI(int numThreadsForMessageType, int numRunsForMessageType, DataObj dataObj){ this.numThreadsForMessageType = numThreadsForMessageType; this.numRunsForMessageType = numRunsForMessageType; […]

启用JSP Custom taglib以使用spring服务bean

我正在使用Spring MVC 3.2.4(Spring Core 3.2.4)开发一个Web应用程序,后端使用jpa和hibernate。 目前正在使用Tomcat v6.0进行测试。 我有一个案例,当我创建一个JSP自定义标记库lib(使用jsp-api 2.1.1和servlet-api 2.5),这是一个自定义查找下拉列表,我会给它查找类型,它将从将此类型下的项目DB作为列表中的项目进行渲染。 自定义taglib类基本上看起来像这样: public class LookupsTag extends SimpleTagSupport { @Autowired private static LookupService lookupService; private String type; public void doTag() throws JspException, IOException { List items = lookupService.findByType(getType()); StringBuffer buff = new StringBuffer(); buff.append(“”); //…adding items… buff.append(“”); getJspContext().getOut().write(buff.toString()); } //getters and setters } 我已经相应地创建了tld文件。 一旦我尝试使用此自定义标记查看页面,就会抛出NullPointerException ,因为在doTag()方法中, lookupService实例为null […]

spring – 如何自动连接数据源?

我总是遇到autowire和DI的问题,所以我希望有人可以帮忙,因为我已经被困了几天了。 这是代码: @Service public class TicketsController implements Controller { private TicketManager ticketManager; @Autowired public void setTicketManager(TicketManager ticketManager) { this.ticketManager = ticketManager; } … } @Service public class SimpleTicketManager implements TicketManager { private TicketsDao ticketsDao; @Autowired public void setTicketsDao(TicketsDao ticketsDao) { this.ticketsDao = ticketsDao; } … } @Repository public class JdbcTicketDao implements TicketsDao { private DataSource […]

使对象弹簧管理

如何管理已存在的对象弹簧? 我想使用aspectj将它连接到Springs AoPfunction。 我知道这是一个挑战,因为Spring AoP使用可能与对象一起创建的动态代理。 我为什么需要这个? 我有一个第三方类,它接受一个只在运行时知道的构造函数参数,因此我似乎无法将它添加到我的applicationContext或使用spring FactoryBean接口进行构造。 还有别的办法吗? 我已经尝试了以下但没有取得很大的成功: Obj obj = new ThirdPartyObj(“runtime constructor arg”); appContext.getAutowireCapableBeanFactory().initializeBean(obj, “Obj”); 它可能是弹簧管理的,但我仍然不能用它来触发方面。 [编辑] axtavt指出问题是我不使用从initializeBean(..)返回的对象。 提到的两种方法都有效,但前提是: 使用接口ObjInterface obj = (ObjInterface) ac.getBean(“obj”, args); 或者我们会得到一个: java.lang.ClassCastException: $Proxy28 cannot be cast to com.company.Obj 不使用接口但启用CGLIB 。 这需要一个非私有的默认构造函数,否则我们将获得: java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given

在没有弹簧实例化的类中支持自动assembly(3)

我意识到这应该是非常基本的,但我还没有在Helloworld之后找到第二步 所以我拥有的是: spring config xml名为spring-beans.xml: 弹簧上下文初始化类: public static void main(String[] args) { // initialize Spring ApplicationContext context = new ClassPathXmlApplicationContext(“META-INF/spring-beans.xml”); App app = (App) context.getBean(“app”); app.run(); } AppImpl类的相关细节: @Component(“app”) public final class AppImpl implements App{ // focus of question: This autowiring works @Autowired private DAO1 dao1; public void run() { //focus of question: This works […]

spring – ApplicationContext registerBean自动assembly失败,但getBean在Spring 5中工作

我正在使用一个使用动态bean注册的配置类: @Configuration public class ConfigClass { @Autowired private GenericApplicationContext applicationContext; @PostConstruct private void init() { System.out.println(“init”); applicationContext.registerBean(“exService”, ExecutorService.class, () -> Executors.newFixedThreadPool(10), bd -> bd.setAutowireCandidate(true)); System.out.println(“init done”); } } 如果我尝试自动assemblybean,则应用程序启动失败, Field exService in com.example.DemoApplication required a bean of type ‘java.util.concurrent.ExecutorService’ that could not be found.出现错误Field exService in com.example.DemoApplication required a bean of type ‘java.util.concurrent.ExecutorService’ that could […]

将请求范围的bean注入另一个bean

我想创建一个在请求生命周期中唯一的UUID。 为此,我使用@Scope(“request”)注释创建一个UUID bean。 @Bean @Scope(scopeName = WebApplicationContext.SCOPE_REQUEST) public UUID requestUUID() { return UUID.randomUUID(); } 我想在我的控制器中访问这个bean。 所以我用@Autowired注入它。 这很好用。 @Controller public class DashboardController { @Autowired UUID uuid; @Autowired WelcomeMessageService welcomeMessageService; @Autowired IssueNotificationService issueNotificationService; @RequestMapping(“/”) public String index(Model model) throws InterruptedException, ExecutionException { System.out.println(uuid); PortalUserDetails userLog = getPortalUserDetails(); BusinessObjectCollection welcomeMessages = welcomeMessageService.findWelcomeMessages( 20, 0, userLog.getZenithUser(), userLog.getConnectionGroup().getConnectionGroupCode(), “FR”); if(welcomeMessages!=null) […]