gpt4 book ai didi

java - 将服务的 getBean 替换为 @Autowired

转载 作者:行者123 更新时间:2023-12-05 07:57:34 26 4
gpt4 key购买 nike

这里我有一个名为 RegionsServiceImpl 的带有 @Service 的主类。我正在使用 ApplicationContext.getBean 对其进行初始化,但我想使用 @Autowired。当我这样做时,@Autowired 不会初始化它。

主类:

package com.rebel.shop;

public class JpaRepoTest {

//ApplicationContext ctx;

@Autowired
RegionsServiceImpl regionsServiceImpl;

public JpaRepoTest() {
// ctx = new AnnotationConfigApplicationContext(DataConfig.class);
// regionsServiceImpl = ctx.getBean("regionsServiceImpl", RegionsServiceImpl.class);
}

public static void main(String[] args) {
JpaRepoTest jpaRepoTest = new JpaRepoTest();
jpaRepoTest.testService();
}

private void testService() {
System.out.println(regionsServiceImpl.findById(3l).getName());
}
}

我的服务类:

package com.rebel.shop.persistence.jpa.service;

@Service
public class RegionsServiceImpl implements RegionsService {

@Resource
private RegionsRepository regionsRepository;

@Override
public Regions findById(long id) {
return regionsRepository.findOne(id);
}
}

它的界面:

package com.rebel.shop.persistence.jpa.service;

public interface RegionsService {
public Regions findById(long id);
}

repo :

package com.rebel.shop.persistence.jpa.repository;

public interface RegionsRepository extends JpaRepository<Regions, Long> {
}

和 Spring 的 Java 配置:

package com.rebel.shop.persistence.jpa.config;

@Configuration
@EnableTransactionManagement
@PropertySource("classpath:app.properties")
@EnableJpaRepositories("com.rebel.shop.persistence.jpa.repository")
@ComponentScan("com.rebel.shop")

public class DataConfig {

private static final String PROP_DATABASE_DRIVER = "db.driver";
private static final String PROP_DATABASE_PASSWORD = "db.password";
private static final String PROP_DATABASE_URL = "db.url";
private static final String PROP_DATABASE_USERNAME = "db.username";
private static final String PROP_HIBERNATE_DIALECT = "db.hibernate.dialect";
private static final String PROP_HIBERNATE_SHOW_SQL = "db.hibernate.show_sql";
private static final String PROP_ENTITYMANAGER_PACKAGES_TO_SCAN = "db.entitymanager.packages.to.scan";
private static final String PROP_HIBERNATE_HBM2DDL_AUTO = "db.hibernate.hbm2ddl.auto";

@Resource
private Environment env;

@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();

dataSource.setDriverClassName(env.getRequiredProperty(PROP_DATABASE_DRIVER));
dataSource.setUrl(env.getRequiredProperty(PROP_DATABASE_URL));
dataSource.setUsername(env.getRequiredProperty(PROP_DATABASE_USERNAME));
dataSource.setPassword(env.getRequiredProperty(PROP_DATABASE_PASSWORD));

return dataSource;
}

@Bean
public PlatformTransactionManager transactionManager() {

JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory());
return txManager;
}

@Bean
public HibernateExceptionTranslator hibernateExceptionTranslator() {
return new HibernateExceptionTranslator();
}

@Bean
public EntityManagerFactory entityManagerFactory() {

HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setShowSql(false);
vendorAdapter.setGenerateDdl(false);
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("com.rebel.shop.persistence.entity");
factory.setDataSource(dataSource());
factory.afterPropertiesSet();

return factory.getObject();
}

// @Bean
// RegionsServiceImpl regionsServiceImpl() {
// return new RegionsServiceImpl();
// }
}

提前致谢!

UPD1:

异常(exception):

Exception in thread "main" java.lang.NullPointerException
at com.rebel.shop.JpaRepoTest.testService(JpaRepoTest.java:33)
at com.rebel.shop.JpaRepoTest.main(JpaRepoTest.java:29)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

最佳答案

您没有使用 spring 容器,而是使用 new 实例化对象,因此不会 Autowiring spring bean,如下修改您的 bean

package com.rebel.shop;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(DataConfig.class)
public class JpaRepoTest {

//ApplicationContext ctx;

@Autowired
RegionsServiceImpl regionsServiceImpl;

public JpaRepoTest() {
// ctx = new AnnotationConfigApplicationContext(DataConfig.class);
// regionsServiceImpl = ctx.getBean("regionsServiceImpl", RegionsServiceImpl.class);
}

@Test
public void mainMethod() {
testService();
}

private void testService() {
System.out.println(regionsServiceImpl.findById(3l).getName());
}
}

关于java - 将服务的 getBean 替换为 @Autowired,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26747473/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com