Исключение при сборке Autowired(required=true)
Приветствую уважаемые форумчане.
Вот читаю документацию Spring Security, и дошел читать до пункта [B]10.1 In-Memory Authentication[/B] [URL=«https://docs.spring.io/spring-security/site/docs/5.2.2.RELEASE/reference/htmlsingle/#jc-authentication-inmemory»]https://docs.spring.io/spring-security/site/docs/5.2.2.RELEASE/reference/htmlsingle/#jc-authentication-inmemory[/URL] Всё нормально, проблем нет.
А вот чуть ниже, пункт [B]10.2 JDBC Authentication[/B] [URL=«https://docs.spring.io/spring-security/site/docs/5.2.2.RELEASE/reference/htmlsingle/#jc-authentication-jdbc»]https://docs.spring.io/spring-security/site/docs/5.2.2.RELEASE/reference/htmlsingle/#jc-authentication-jdbc[/URL] для меня есть проблема. А именно:
При запуске проекта получаю исключение:
[B]ИСКЛЮЧЕНИЕ:[/B]
[SPOILER][JAVA]17-Feb-2020 20:40:28.418 WARNING [RMI TCP Connection(3)-127.0.0.1] org.springframework.context.support.AbstractApplicationContext.refresh Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘securityConfig’: Unsatisfied dependency expressed through field ‘dataSource’; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘javax.sql.DataSource’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}[/JAVA][/SPOILER]
Не могу понять почему она появляется, вроде всё норм.
Мой примитивный проект выглядит следующим образом:
[B][COLOR="blue"]package[/COLOR] com.mysprinttestsecurity.config;[/B]
[B]FreeMarkerConfig.class[/B] [SPOILER][JAVA]import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
@Configuration
@ComponentScan({«com.mysprinttestsecurity»})
public class FreeMarkerConfig {
@Bean
public FreeMarkerConfigurer freeMarkerConfigurer() {
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setTemplateLoaderPath("/WEB-INF/templates");
configurer.setDefaultEncoding("UTF-8");
return configurer;
}
@Bean
public ViewResolver getViewResolver(){
FreeMarkerViewResolver viewResolver = new FreeMarkerViewResolver();
viewResolver.setSuffix(".ftl");
viewResolver.setContentType("text/html;charset=UTF-8");
viewResolver.setCache(false);
return viewResolver;
}
}[/JAVA][/SPOILER]
[B]MessageWebApplicationInitializer.class[/B] [SPOILER][JAVA]import com.mysprinttestsecurity.security.SecurityConfig; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class MessageWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] { RootConfiguration.class, SecurityConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { FreeMarkerConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}[/JAVA][/SPOILER]
[B]RootConfiguration.class[/B] [SPOILER][JAVA]import com.mysprinttestsecurity.security.SecurityConfig; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import;
@Configuration
@Import(SecurityConfig.class)
public class RootConfiguration {
}[/JAVA][/SPOILER]
[B]SQLConfig.class[/B] [SPOILER][JAVA]import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
@Configuration
@ComponentScan(«com.mysprinttestsecurity.dao»)
public class SQLConfig {
@Bean
public JdbcTemplate getJdbcTemplate() {
return new JdbcTemplate(dataSource());
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
driverManagerDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
driverManagerDataSource.setUrl("jdbc:mysql://localhost:3306/mysecurity?serverTimezone=UTC");
driverManagerDataSource.setUsername("root");
driverManagerDataSource.setPassword("1234");
return driverManagerDataSource;
}
}[/JAVA][/SPOILER]
[B][COLOR="Blue"]package[/COLOR] com.mysprinttestsecurity.controller;[/B]
[B]HomeController.class[/B] [SPOILER][JAVA]import com.mysprinttestsecurity.dao.DaoUser; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HomeController {
@Autowired
DaoUser daoUser;
@GetMapping("/")
public String home () {
System.out.println(daoUser.getByName("vasa").getName().toString());
return "/home";
}
}[/JAVA][/SPOILER]
[B][COLOR="blue"]package[/COLOR] com.mysprinttestsecurity.dao;[/B]
[B]DaoUser.class[/B] [SPOILER][JAVA]import com.mysprinttestsecurity.entity.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import org.springframework.jdbc.core.JdbcTemplate;
@Repository
public class DaoUser {
@Autowired
public JdbcTemplate jdbcTemplate;
public User getByName(String name) {
String sql = "SELECT * FROM user WHERE name=?";
return jdbcTemplate.queryForObject(sql, new UserMapper(), name);
}
}[/JAVA][/SPOILER]
[B]UserMapper.class[/B] [SPOILER][JAVA]import com.mysprinttestsecurity.entity.User; import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet; import java.sql.SQLException;
public class UserMapper implements RowMapper {
public User mapRow(ResultSet resultSet, int i) throws SQLException {
User user = new User();
user.setName(resultSet.getString("name"));
user.setPassword(resultSet.getString("password"));
user.setRole(resultSet.getString("role"));
return user;
}
}[/JAVA][/SPOILER]
[B][COLOR="blue"]package [/COLOR]com.mysprinttestsecurity.entity;[/B]
[B]User.class[/B] [SPOILER][JAVA]public class User {
private int id;
private String name;
private String surname;
private String email;
private String password;
private String role;
public User() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
}[/JAVA][/SPOILER]
[B][COLOR="blue"]package [/COLOR]com.mysprinttestsecurity.security;[/B]
[B]MessageSecurityWebApplicationInitializer.class[/B] [SPOILER][JAVA]import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
public class MessageSecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer { }[/JAVA][/SPOILER]
[B]SecurityConfig.class[/B] [SPOILER][JAVA]import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.core.userdetails.User;
import javax.sql.DataSource;
@EnableWebSecurity
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
public DataSource dataSource;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
// ensure the passwords are encoded properly
User.UserBuilder user = User.builder();
auth
.jdbcAuthentication()
.dataSource(dataSource)
.withDefaultSchema()
.withUser(user.username("user").password("{noop}password").roles("USER"))
.withUser(user.username("admin").password("{noop}password").roles("USER","ADMIN"));
}
}[/JAVA][/SPOILER]
[B][COLOR="Red"]POM.XML[/COLOR][/B] [SPOILER][XML]
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${encoding}</encoding>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<failOnMissingWebXml>false</failOnMissingWebXml>
<org.springframework.security>5.2.2.RELEASE</org.springframework.security>
<org.springframework>5.2.3.RELEASE</org.springframework>
<java.version>1.8</java.version>
<encoding>UTF-8</encoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${org.springframework.security}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${org.springframework.security}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.29</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${org.springframework}</version>
</dependency>
</dependencies>[/XML][/SPOILER]
Помогите пожалуйста сделать что бы запускался. Spring Boot не интересует. Интересует Spring MVC
Буду очень признателен.