Why is #PostConstruct recognised in my case? - java

Could you tell me why does a method annotated with #PostContruct runs in my case? As far as I know, a method with #PostContruct is processed by Bean Post Processors. If you want to activate the default CommonAnnotationBeanPostProcessor you need to add <context:annotation-config/> in the XML configuration but I want to use only annotation config. In my case, #ComponentScan in configuration is pointed to the service. It means that only classes from this package candidates to be instantiated.
Configuration class:
package config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
#Configuration
#ComponentScan(basePackages = "service")
public class AppConfig {
}
Simple class:
package service;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
#Component
public class Simple {
#PostConstruct
private void sout(){
System.out.println("SOUT");
}
}
And the launcher:
import config.AppConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import service.Simple;
public class Launcher {
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
Simple simple = ctx.getBean("simple", Simple.class);
}
}
The output of the app is "SOUT". Could you explain be who invokes the #PostContruct method and how?

AnnotationConfigApplicationContext is an alternative to XML based configurations. When you use it to create objects, it first creates the object and the #Autowired properties, and then calls the #PostConstruct method. It is a handy alternative to writing a setup() or init() method which you would have to call yourself.

Related

How to create a repository instance in main method of Spring Boot?

I need to set properties for SSL to enable HTTPS in my Spring Boot application's main method. My code looks like this:
import com.our.Task.configuration.FileStorageProperties;
import com.our.Task.entities.ApplicationHttpsSettingsEntity;
import com.our.Task.repository.ApplicationHttpsSettingsEntityRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;
import java.util.Properties;
#SpringBootApplication
#EnableConfigurationProperties({FileStorageProperties.class})
public class OurTaskApplication
{
#Autowired
static ApplicationHttpsSettingsEntityRepository applicationHttpsSettingsEntityRepository;
public static void main(String[] args)
{
ApplicationHttpsSettingsEntity applicationsHttpsSettingsEntity = applicationHttpsSettingsEntityRepository.getById(44);
SpringApplication application = new SpringApplication(OurTaskApplication.class);
Properties properties = new Properties();
if (applicationsHttpsSettingsEntity.getUseHttps() > 0)
{
properties.put("server.ssl.key-store", applicationsHttpsSettingsEntity.getKeyStore());
properties.put("server.ssl.key-store-password", applicationsHttpsSettingsEntity.getKeyStorePassword());
properties.put("server.ssl.key-store-type", applicationsHttpsSettingsEntity.getKeyStoreType());
properties.put("server.ssl.key-alias", applicationsHttpsSettingsEntity.getKeyAlias());
}
// SpringApplication.run(OurTaskApplication.class, args);
application.setDefaultProperties(properties);
ConfigurableApplicationContext ctx = application.run(args);
}
}
It gives me warning that #Autowired is not allowed on static methods. When executed it gives null exception.
I have read this
Can't use #Autowired JPA Repository in Main method of Spring Boot application
But it doesn't give any info on how I can do this. I don't want to use properties file.
I wouldn't do something like this in the main method. Let Spring run and then add your configurations.
I would create a runner class that will do whatever I need once the Spring context is set up.
Example:
#Component
#AllArgsConstructor
public class StartRunner implements ApplicationRunner {
/* Add whatever Bean you need here and autowire them through the constructor or with #Autowired */
#Override
public void run(ApplicationArguments args) throws Exception {
// Do whatever you need here inside
}
}

Spring Annotations Import Config not called

I am trying to make an application that uses Spring annotations to import the configurations. For this question i narrowed it down to two files. The Startup class:
package core;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
#Slf4j
#Configuration
#Import(ConfigSettings.class)
public class Startup {
public static void main (String args[]) {
log.info("main class");
}
}
and the ConfigSettings
package core;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
#Slf4j
#Configuration
#ComponentScan({"connections", "filter"})
#PropertySource({"classpath:config/${env.config:dev}.application.properties"})
public class ConfigSettings {
public ConfigSettings() {
log.info("Constructor ConfigSettings");
}
}
I expected the outcome to be:
[INFO]Constructor ConfigSettings
[INFO]main class
But it only shows mainclass. It looks like the constructor of the config settings is not called at all. I expect it to call it because of the import annotation.
Can anyone explain what is going wrong? Thank you in advance!
Your best bet is to make the config class return config object that contains your values. Generally I don't tend to add an all-encompassing config object, but have a config file for each component (database, controllers, etc...).
You can then return the configured object as a bean and let spring inject it. If I were to make a config file for a RestTemplate (as a simple example):
#Service
public class RestClientConfig {
#Value("${your.config.value}")
private String yourValue;
private final RestTemplate restTemplate = new RestTemplate();
#Bean
public RestTemplate restTemplate() {
// Configure it, using your imported values
// ...
return restTemplate;
}
}
However, the main method is outside of the spring container and you won't be able to bootstrap it that way, but with the above method you can call the configured component directly where you need to use it.

No bean named '' is defined [duplicate]

This question already has an answer here:
Is it possible to set a bean name using annotations in Spring Framework?
(1 answer)
Closed 4 years ago.
I am testing out simple AOP use case in Spring but am getting the below error,
Exception in thread "main"
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
bean named 'bean1' is defined
Below are my source files,
DemoConfig.java
package com.luv2code.aopdemo;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import com.luv2code.aopdemo.aspect.MyDemoLoggingAspect;
import com.luv2code.aopdemo.dao.AccountDAO;
#Configuration
#EnableAspectJAutoProxy
#ComponentScan("com.luv2code.aopdemo")
public class DemoConfig {
#Bean
#Qualifier("bean1")
public AccountDAO accDao() {
return new AccountDAO();
}
#Bean
#Qualifier("bean2")
public MyDemoLoggingAspect myAscpect() {
return new MyDemoLoggingAspect();
}
}
MyDemoLoggingAspect.java
package com.luv2code.aopdemo.aspect;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
#Aspect
public class MyDemoLoggingAspect {
// this is where we add all of our related advices for logging
// let's start with an #Before advice
#Before("execution(** com.luv2code.aopdemo.dao.AccountDAO.addAccount(..))")
public void beforeAddAccountAdvice() {
System.out.println("\n=====>>> Executing #Before advice on addAccount()");
}
}
MainDemoApp.java
package com.luv2code.aopdemo;
import com.luv2code.aopdemo.dao.AccountDAO;
public class MainDemoApp {
public static void main(String[] args) {
// read spring config java class
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DemoConfig.class);
// get the bean from spring container
AccountDAO theAccountDAO = context.getBean("bean1", AccountDAO.class);
// call the business method
theAccountDAO.addAccount();
// do it again!
System.out.println("\nlet's call it again!\n");
// call the business method again
theAccountDAO.addAccount();
// close the context
context.close();
}
}
I have given my bean ID "bean1", even after that Spring is not able to find my bean in the context. Why am I getting this error and how to resolve this?
The #Qualifier tag is used with the #Autowired annotation.
What you need is
#Bean(name="bean1")
public AccountDAO accDao() {
return new AccountDAO();
}

get a null object after used #autowired to DI

I have a utils class that use #Autowired to inject a repository using spring-boot-starter-data-jpa. But when I used this repository to access the database, it said the repository is null. I used the same method in my controller and it works well. And here is my Utils.class
package com.example.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import com.example.dao.RuleRepository;
import com.example.model.Project;
import com.example.model.Rule;
public class Judge {
#Autowired
RuleRepository ruleRepository;
public boolean ageJudge(Project project) {
try {
if (ruleRepository == null)
{
System.out.println("yes");
}else {
System.out.println("false");
}
return false;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
}
Here is my Application.java
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
#SpringBootApplication
#ComponentScan(basePackages = {"com.example"})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
This is the RuleRepository.java
package com.example.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import com.example.model.Project;
import com.example.model.Rule;
public interface RuleRepository extends JpaRepository<Rule, Integer>{
Rule findById(Integer id);
Rule findByRuleName(String ruleName);
}
It is the directory.
The RuleRepository works well in controller. So, what is the problem?
Your util class Judge is a plain POJO not a Spring bean and you can only inject Spring beans inside another Spring beans not Plain POJOs.
If you wish to use your ruleRepository bean inside Judge then make it a Spring component using #Component annotation:
#Component
public class Judge {
#Autowired
RuleRepository ruleRepository;
..............................
}
User #Service annotation of Judge class is acting as business logic implementation class.
Your Judge should be annotated #Component
#Component
public class Judge{
// ...
}
so that Spring will instantiate a Judge bean and it will be available for injection. You can then use that judge bean in any managed bean (e.g: a controller)
// SomeController
#Autowired
Judge judge;
But if you instantiate judge object your self, like this:
Judge judge2 = new Judge();
your repository will be null, be cause Spring have nothing to do with judge2 object, it is not managed by Spring.
You need to make your Judge class at least a #Component of your project, which will make your class managed by Spring, therefore your RuleRepository will be instantiated.
If it doesn't work on first try, you will have to add your com.example.controller package in the list of packages to scan, in the #ComponentScan annotation
First as everyone mention your class Judge does not have #Component annotations.
The other thing is, maybe my Spring is getting little bit rusty.
But as far as I remember, I think your repository also require to have #Component or #Repository annotation

No qualifying bean of type is defined when running a main method

I've seen a lot of questions about this error before, but no resolution that works for me.
I'm new to Spring, but trying to use the Spring Data for Neo4J library for a project. I decided to start with a quick spike to make sure I know how everything is working, and so I set up a simple App class with a main method like so:
package org.example.neo4jSpike;
import org.example.neo4jSpike.domain.Actor;
import org.example.neo4jSpike.repositories.ActorRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;
/**
* Hello world!
*
*/
#Component
public class App
{
#Autowired
private ActorRepository actors;
#SuppressWarnings("resource")
public static void main( String[] args )
{
ApplicationContext context = new AnnotationConfigApplicationContext(SpikeConfiguration.class);
App a = context.getBean(App.class);
a.init();
}
private void init(){
Actor michaelDouglas = actors.save(new Actor("Michael Douglas"));
System.out.println( "Hello World!" );
System.out.println(michaelDouglas.getId());
System.out.println("Total people: " + actors.count());
}
}
I have the configuration class setup as well:
package org.example.neo4jSpike;
import org.neo4j.ogm.session.Session;
import org.neo4j.ogm.session.SessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.data.neo4j.config.Neo4jConfiguration;
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;
#Configuration
#EnableNeo4jRepositories(basePackages = "org.example.neo4jSpike.repositories")
#EnableTransactionManagement
public class SpikeConfiguration extends Neo4jConfiguration{
#Bean
public SessionFactory getSessionFactory() {
// with domain entity base package(s)
return new SessionFactory("org.example.neo4jSpike.domain");
}
// needed for session in view in web-applications
#Bean
#Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
public Session getSession() throws Exception {
return super.getSession();
}
}
I'll add the code for my repositories and domain classes if needed, but they're all set up in a similar manner, and are all pretty simple.
When I try and run the main, however, I get
No qualifying bean of type [org.example.neo4jSpike.App] is defined
I don't see how it's not defined, it's right there, defined as an #Component. What am I misunderstanding?
Doesn't matter if you put the #Component annotation if Spring is not scanning your class package. You can add a #ComponentScan annotation in you configuration class and configure it to scan the package where your App class is located. Alternatively you can remove the #Component annotation and declare a Bean of type App in the configuration class.
Hope this can help.

Categories

Resources