Java Spring启动应用程序无法启动,因为找不到自动连线bean
我的应用程序:-Java Spring启动应用程序无法启动,因为找不到自动连线bean,java,spring-mvc,spring-boot,Java,Spring Mvc,Spring Boot,我的应用程序:- @SpringBootApplication @ComponentScan("fi.ma.vegshopping") @EnableJpaRepositories("fi.ma.vegshopping") @EntityScan(basePackages = "fi.ma.vegshopping") public class VegShoppingApplication { public static void main
@SpringBootApplication
@ComponentScan("fi.ma.vegshopping")
@EnableJpaRepositories("fi.ma.vegshopping")
@EntityScan(basePackages = "fi.ma.vegshopping")
public class VegShoppingApplication {
public static void main(String[] args) {
SpringApplication.run(VegShoppingApplication.class, args);
}
}
问题类别:-
import fi.ma.vegshopping.dao.AccountDAO;
import fi.ma.vegshopping.entity.Account;
@Service
public class MyDBAuthenticationService implements UserDetailsService {
@Autowired
private AccountDAO accountDAO;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
Account account = accountDAO.findAccount(username);
System.out.println("Account= " + account);
if (account == null) {
throw new UsernameNotFoundException("User " //
+ username + " was not found in the database");
}
// EMPLOYEE,MANAGER,..
String role = account.getUserRole();
List<GrantedAuthority> grantList = new ArrayList<GrantedAuthority>();
// ROLE_EMPLOYEE, ROLE_MANAGER
GrantedAuthority authority = new SimpleGrantedAuthority("ROLE_" + role);
grantList.add(authority);
boolean enabled = account.isActive();
boolean accountNonExpired = true;
boolean credentialsNonExpired = true;
boolean accountNonLocked = true;
UserDetails userDetails = (UserDetails) new User(account.getUserName(), //
account.getPassword(), enabled, accountNonExpired, //
credentialsNonExpired, accountNonLocked, grantList);
return userDetails;
}
}
AccountDAOImpl类:-
package fi.ma.vegshopping.impl;
import javax.transaction.Transactional;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import fi.ma.vegshopping.dao.AccountDAO;
import fi.ma.vegshopping.entity.Account;
@Transactional
public class AccountDAOImpl implements AccountDAO {
@Autowired
private SessionFactory sessionFactory;
@Override
public Account findAccount(String userName ) {
Session session = sessionFactory.getCurrentSession();
Criteria crit = session.createCriteria(Account.class);
crit.add(Restrictions.eq("userName", userName));
return (Account) crit.uniqueResult();
}
}
错误:-
应用程序无法启动
说明: fi.ma.vegshopping.authentication.MyDBAuthenticationService中的字段accountDAO需要找不到类型为“fi.ma.vegshopping.dao.accountDAO”的bean 行动:
考虑在您的配置中定义“fi.ma.vegshopping.dao.AccountDAO”类型的bean。问题可能在于包结构,您需要将
@Component
标记到AccountDAOImpl
类(在fi.ma.vegshopping.impl
包下)如下所示,以便容器可以扫描并找到要注入的bean
@Component //or @Repository
public class AccountDAOImpl implements AccountDAO {
//add your code here
}
尝试在您的帐户daoimpl上添加
@Repository
。我不明白您为什么不理解异常消息。它清楚地表明,没有类型为AccountDAO
的Spring管理bean,并且您还没有向AccountDAOImpl
类添加任何Anotion使其成为一个。异常和解决方案都很明显。我在AccountDAOImpl类中尝试了@Component和@Repository。我还在VegShoppingApplication类中尝试了@ComponentScan(“fi.ma.vegshopping.impl”)@EnableJpaRepositories(“fi.ma.vegshopping.impl”)@EntityScan(basePackages=“fi.ma.vegshopping.impl”)。什么都不管用。仍然是相同的错误。我在AccountDAOImpl类中尝试了@Component和@Repository。我还在VegShoppingApplication类中尝试了@ComponentScan(“fi.ma.vegshopping.impl”)@EnableJpaRepositories(“fi.ma.vegshopping.impl”)@EntityScan(basePackages=“fi.ma.vegshopping.impl”)。什么都不管用。仍然是相同的错误。AccountDAO不应该扩展例如Crudepository?如果您使用的是spring数据jpa,您可以
@Component //or @Repository
public class AccountDAOImpl implements AccountDAO {
//add your code here
}