我有一个有效的项目,然后突然停了下来。春天没有注入
@Configuration
@Order(2)
public static class RegularSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
@Inject
private Environment env;
@Inject
private AjaxAuthenticationSuccessHandler ajaxAuthenticationSuccessHandler;
//Could not autowire. No bean of name 'ajaxAuth..' type found
...
AjaxAuthenticationSuccessHandler
类看起来像这样
@Component
public class AjaxAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
@Autowired
private UserRepository userRepository;
...
我发现facet已配置,但main
方法和@ComponentScan
文件的行是红色的。
这与我的问题有关吗?怎么告诉Spring ajaxAuth..
班?即使我切换到以前工作的分支,也会发生这种情况。
以下是Application.java
:
@ComponentScan
@EnableAutoConfiguration(exclude = {MetricFilterAutoConfiguration.class, MetricRepositoryAutoConfiguration.class})
public class Application {
private static final Logger log = LoggerFactory.getLogger(Application.class);
@Inject
private Environment env;
@PostConstruct
public void initApplication() {
if (env.getActiveProfiles().length == 0) {
log.warn("No Spring profile configured, running with default configuration");
} else {
log.info("Running with Spring profile(s) : {}", Arrays.toString(env.getActiveProfiles()));
}
}
/**
* Main method, used to run the application.
*/
public static void main(String[] args) throws UnknownHostException, NoSuchAlgorithmException {
if (Cipher.getMaxAllowedKeyLength("AES") <= 128) {
log.error("You need Java Cryptography Extension (JCE) to run the system");
return;
}
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
System.setProperty("user.timezone", "UTC");
SpringApplication app = new SpringApplication(Application.class);
app.setShowBanner(false);
SimpleCommandLinePropertySource source = new SimpleCommandLinePropertySource(args);
// Check if the selected profile has been set as argument.
// if not the development profile will be added
addDefaultProfile(app, source);
addLiquibaseScanPackages();
Environment env = app.run(args).getEnvironment();
// System.setProperty("javax.net.ssl.trustStore", env.getProperty("server.ssl.key-store"));
// System.setProperty("javax.net.ssl.trustStorePassword", env.getProperty("server.ssl.key-store-password"));
String httpPort = env.getProperty("httpPort");
String httpsPort = env.getProperty("server.port");
String div = "----------------------------------------------------------";
String local = "\tLocal: \t\thttps://127.0.0.1:" + httpsPort;
String external = "\tExternal: \thttps://" + InetAddress.getLocalHost().getHostAddress() + ":" + httpsPort;
if (httpPort != null) {
div += "-----------------------------";
local += " & http://127.0.0.1:" + httpPort;
external += " & http://" + InetAddress.getLocalHost().getHostAddress() + ":" + httpPort;
}
log.info("Access URLs:\n" + div + "\n" + local + "\n" + external + "\n" + div);
}
/**
* Set a default profile if it has not been set
*/
private static void addDefaultProfile(SpringApplication app, SimpleCommandLinePropertySource source) {
if (!source.containsProperty("spring.profiles.active")) {
app.setAdditionalProfiles(Constants.SPRING_PROFILE_DEVELOPMENT);
}
}
/**
* Set the liquibases.scan.packages to avoid an exception from ServiceLocator.
*/
private static void addLiquibaseScanPackages() {
System.setProperty("liquibase.scan.packages", Joiner.on(",").join(
"liquibase.change", "liquibase.database", "liquibase.parser",
"liquibase.precondition", "liquibase.datatype",
"liquibase.serializer", "liquibase.sqlgenerator", "liquibase.executor",
"liquibase.snapshot", "liquibase.logging", "liquibase.diff",
"liquibase.structure", "liquibase.structurecompare", "liquibase.lockservice",
"liquibase.ext", "liquibase.changelog"));
}
}
答案 0 :(得分:1)
我在JHipster生成的Spring Boot应用程序类中遇到了同样的问题,直到我在类中添加了@Configuration
注释:
@Configuration
@ComponentScan
@EnableAutoConfiguration(exclude = {MetricFilterAutoConfiguration.class,
MetricRepositoryAutoConfiguration.class})
public class Application {
/* ... */
}
这三个注释可以替换为单个@SpringBootApplication
注释:
@SpringBootApplication(exclude = {MetricFilterAutoConfiguration.class,
MetricRepositoryAutoConfiguration.class})
public class Application {
/* ... */
}
两者似乎都适用于IDEA 2017.1。
答案 1 :(得分:0)
似乎解决主要课程Application.java
时出现问题。只需尝试删除并重新添加Spring facet(您还需要另外重新创建手动上下文)。