Spring不会初始化我的@Configuration -classes

时间:2017-09-24 09:38:14

标签: java spring

我的Spring应用程序有点问题。 我已经使用@Configuration-annotated类配置了不同的东西,当我在Netbeans中运行应用程序时,一切似乎都运行正常。但是现在我想将我的应用程序构建到可执行jar文件中,它不再起作用了。好像它没有找到任何带注释的类。

感谢您的帮助。

主要课程:

@SpringBootApplication
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class StockServerApplication
{
    public static void main(String[] args) throws IOException
    {
        // Check if settings exists
        if(!AppSettings.settingsFileExists()){
            AppSettings.writeExampleSettingsFile();
            System.out.println("Settings-file not found. Example file has been written in the root-dir\nClosing...");
            System.exit(0);
        }

        AppSettings.initialize();

        if(!AppSettings.isValid()){
            System.exit(0);
        }

        SpringApplication app = new SpringApplication(StockServerApplication.class);

        Properties props = AppSettings.settingsToNativeProperties(AppSettings.applicationSettings);
        props.put("spring.thymeleaf.mode", "LEGACYHTML5");
        props.put("spring.thymeleaf.cache", "false");
        props.put("spring.jpa.database", "default");
        // Debug
       // props.put("debug", "true");
        app.setDefaultProperties(props);
        app.run(args);
    }

    @Autowired
    private AuthorizedUserRepository repo;

    @PostConstruct
    private void createAdmin(){

        AuthorizedUser user = repo.getByUsername("Admin");
        if(user == null){
            // Generate admin-user
            String possiblePasswordCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
            String generatedPassword = RandomStringUtils.random(10, possiblePasswordCharacters);
            user = new AuthorizedUser(null, null, "Admin", generatedPassword, null, AuthorizedUserRole.Admin);
            repo.save(user);
            System.out.println("\n\nCreated Admin-account with password: " + generatedPassword + "\n\n");
        }
    }
}

例如,这里是我的SecurityConfiguration类,它根本没有初始化。

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter
{
    @Autowired
    private AuthenticationFilter filter;

    @Override
    protected void configure(HttpSecurity http) throws Exception
    {
        Settings.ServerSettings set = AppSettings.applicationSettings.getServerSettings();
        if(!set.isUseAuthentication())
            return;

        System.out.println("Initializing security-settings");

        http.csrf().disable().authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/utils/**").permitAll()
                .antMatchers("/app/**").permitAll()
                .antMatchers("/authorize").permitAll()
                .antMatchers(HttpMethod.POST, "/users/add/*").hasAuthority(AuthorizedUserRole.Admin.toString())
                .antMatchers(HttpMethod.DELETE, "/users/delete/*").hasAuthority(AuthorizedUserRole.Admin.toString())
                .antMatchers(HttpMethod.GET, "/users/all/*").hasAuthority(AuthorizedUserRole.Admin.toString())
                .antMatchers(HttpMethod.PUT, "/users/changerole/*").hasAuthority(AuthorizedUserRole.Admin.toString())
                .anyRequest().authenticated()
                .and()
                .addFilterBefore(filter, UsernamePasswordAuthenticationFilter.class)
                .exceptionHandling()
                .authenticationEntryPoint(new AuthEntryPoint());
    }

    @Bean
    public FilterRegistrationBean authenticationFilterRegistrationBean(){
        FilterRegistrationBean regBean = new FilterRegistrationBean();
        regBean.setFilter(filter);
        regBean.setOrder(1);

        return regBean;
    }
}

这些类都不适用于jar文件。在IDE上一切正常。 如果有人遇到同样的问题但是我没找到任何东西,我试图在最后几天搜索。

pom.xml文件。 我认为这个文件包含很多不必要的依赖。但我认为这不会导致这个问题。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>maja</groupId>
<artifactId>StockServer</artifactId>
<version>0.1</version>
<packaging>jar</packaging>

<name>StockServer</name>
<description>Stock handling server</description>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <version>1.5.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
        <version>1.5.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>1.3.6.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <version>1.5.7.RELEASE</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>mssql-jdbc</artifactId>
        <version>6.2.1.jre8</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.11.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.3.11.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot</artifactId>
        <version>1.5.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.6</version>
    </dependency>
    <!-- Spring Security -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
        <version>1.4.2.RELEASE</version>
    </dependency>

    <!-- JWT -->
    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt</artifactId>
        <version>0.6.0</version>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.0</version>
    </dependency>
    <dependency>
        <groupId>net.sourceforge.nekohtml</groupId>
        <artifactId>nekohtml</artifactId>
        <version>1.9.22</version>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.3.156</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.1.4.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.0-api</artifactId>
        <version>1.0.1.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.1.4.Final</version>
    </dependency>
</dependencies>

<build>
    <plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <executions>
            <execution>
                <goals>
                    <goal>repackage</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    </plugins>
</build>

0 个答案:

没有答案