Tag: autowired

Spring的@Autowired是一个巨大的性能问题吗?

我有一个项目……我不知道…… 200-300 daos / services / controllers我使用@Autowired连接所有内容而不是在applicationContext.xml指定所有内容。 我的问题是,这对我的创业时间有多大影响? 是否值得删除所有@Autowired注释并实际通过applicationContext.xml手动连接此applicationContext.xml ? 从架构的角度来看,我喜欢@Autowired 。 我不想通过使用xml文件添加另一层复杂性 – 就我而言,它没有增加任何价值。 但是,如果这种事情为我的容器的加载时间增加10秒,我可能会考虑它。 如果成本是100毫秒,那么我会保持原样。 谢谢

无法让UserDetailsManager注入Spring Boot和基于Java的配置

我有spring boot webapp,它使用基于Java的配置来配置JdbcUserDetailsManager: @Configuration @EnableWebMvcSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired protected DataSource dataSource; @Autowired public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception { auth.jdbcAuthentication() .dataSource(dataSource) .usersByUsernameQuery(“select username as principal, password as credentials, true from users where username = ?”) .authoritiesByUsernameQuery(“select username as principal, authority as role from authorities where username = ?”) .rolePrefix(“ROLE_”); } @Override […]

Spring @Autowired vs使用’new’关键字创建Object

我正在学习Spring并构建一些实验应用程序。 我很困惑在哪里使用@Autowired来创建Object。 我得到了促进松散耦合的部分,并且每次创建一个新的Object而不是’new’关键字。 但是我们应该如何处理我们需要在我们的应用程序中使用的第三方对象。 例如,我正在使用一个restAPI,我需要初始化三个类,类似这样 RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); HttpEntity entity = new HttpEntity(“parameters”, headers); restTemplate.exchange(url, HttpMethod.POST, entity, String.class); 这段代码正在为RestTemplate,HttpHeaders和HttpEntity创建新的对象。 使用此代码,每次调用其余API时,它都会创建三个新对象。 这是正确的方法还是我应该标记它们@Autowired。 请详细说明。

使用@Autowired将依赖项注入使用“new …”创建的对象中

将bean注入helper类时遇到问题。 它基本上是这样的:我在页面构造函数中创建了一个对象,它可以完成一些工作,返回一些数据并在页面上显示这些数据。 在此辅助对象中,应通过@Autowired批注注入服务。 但是,当我使用它时,我总是得到一个空指针exception。 我也试过@SpringBean但没有帮助。 另一方面,当我使用@SpringBean将此服务直接注入到页面中时,它可以访问并且工作正常。 你知道问题出在哪里吗? 这是页面: public class Page extends BasePage { public Page() { HelperObject object = new HelperObject(new Application(“APP_NAME”)); String result = object.getData(); add(new Label(“label”, result)); } } 助手对象: public class HelperObject { private Application app; @Autowired private Service service; public HelperObject(Application app) { this.app = app; } public String getData() […]

Java Config @Bean未在其他@Configuration类中自动assembly

尝试使用Java Config设置Spring 4 Web应用程序时遇到了将配置类中创建的bean自动assembly到另一个配置类的问题。 ‘dataSource’bean在MyBatisConfig类中具有空值。 这似乎是配置中唯一没有正确连接的bean。 查看Spring调试日志(请参阅下面最后一个代码块中的日志的最后一部分),它看起来正确实例化,但似乎也被破坏了? 我的配置有什么问题? PropertySourcesPlaceholderConfigurerConfig类: package nl.somesite.teamshot.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.core.io.ClassPathResource; @Configuration public class PropertySourcesPlaceholderConfigurerConfig { @Bean public PropertySourcesPlaceholderConfigurer propertyConfigurer() { PropertySourcesPlaceholderConfigurer propertyConfigurer = new PropertySourcesPlaceholderConfigurer(); propertyConfigurer.setLocation(new ClassPathResource(“application.properties”)); /*propertyConfigurer.setLocation(new ClassPathResource(“file:${catalina.home}/conf/application.properties”)); propertyConfigurer.setLocation(new ClassPathResource(“/var/lib/openshift/517874b8e0b8cd218e000391/app-root/data/apache-tomcat-7.0.39/conf/application.properties”));*/ propertyConfigurer.setIgnoreUnresolvablePlaceholders(false); propertyConfigurer.setIgnoreResourceNotFound(true); return propertyConfigurer; } } DbConfig类: package nl.somesite.teamshot.config; import org.apache.commons.dbcp.BasicDataSource; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; […]

@Autowired和@Service在控制器上工作,但不是从不同的包中工作

我需要帮助理解@Autowired和@Service背后的概念。 我有一个用@Service和@Autowired控制器定义的DAO,一切似乎都很好,但是,我在不同的类中使用相同的@Autowired然后它不起作用。 例: 服务 @Service public class MyService { private JdbcTemplate jdbcTemplate; @Autowired public void setDataSource (DataSource myDataSource) { this.jdbcTemplate = new JdbcTemplate(myDataSource); } public void testUpdate(){ jdbcTemplate.update(“some query”); } } 调节器 package com.springtest.mywork.controller; @Controller @RequestMapping(value = “/test.html”) public class MyController { @Autowired MyService myService; @RequestMapping(method = RequestMethod.GET) public String test(Model model) { systemsService.testUpdate(); return […]

Hibernate实体autowire

你可以请教我,我怎样才能很好地为Hibernate实体启用Spring自动assembly? 假设我有一个实体,并希望在那里有邮件发件人: @Entity public class EmailActivity extends Activity { @Autowired @Transient private JavaMailSender javaMailSender; … } 有没有比做更好的方法 AutowireCapableBeanFactory.autowireBean( getCurrentSession().get(Activity.class, id) ); 在我的DAO? 谢谢!

@Autowired注释在Java中的好处

也许,因为我的英语错误,我无法理解使用@Autowired注释的好处。 根据教程,我们可以通过@Autowired将第一个(I。)案例简化为第二个案例(II。)。 我的问题是,@ Autowired是什么意思? 因为它不再告诉,因为不使用@Autowired,编译器可以根据声明得出“EmpDao emDao”和“EmpManager”密切相关。 从这里引用的代码 一世。 public class EmpManager { private EmpDao empDao; public EmpDao getEmpDao() { return empDao; } public void setEmpDao(EmpDao empDao) { this.empDao = empDao; } … } II。 import org.springframework.beans.factory.annotation.Autowired; public class EmpManager { @Autowired private EmpDao empDao; }

如何在Spring中更新@Autowired String bean的值?

我有一个String,我将自动assembly为bean。 String的值通过属性文件设置,并在运行时加载。 我可以validation这一点。 这是我的XML: ${loaded-prop} 在我的应用程序中,我在bean中自动assembly: @Component public class Foo { @Autowired private String loadedProp; } 一切都很花哨。 我有多个组件在这个bean中自动assembly。 我正在尝试做的是,在应用程序运行时,将bean的值更新为其他内容,以便在bean自动assembly的任何地方,它使用最新的值。 是否可以这样做,或者我只是需要在每次想要更改值时重新启动?

Spring:获取某些接口和类型的所有Bean

在我的Spring Boot应用程序中,假设我在Java中有接口: public interface MyFilter (一个很好的例子是Spring的公共接口ApplicationListener ) 我有几个实现,如: @Component public class DesignatedFilter1 implements MyFilter{…} @Component public class DesignatedFilter2 implements MyFilter{…} @Component public class DesignatedFilter3 implements MyFilter{…} 然后,在某些对象中,我有兴趣利用实现MyFilter 的所有filter 但不是 MyFilter 这会是什么语法?