property read from application.properties returning null - spring-boot

I am new to springboot and i am trying to read the property value from the application.properties file in the location(src/main/resources). But it always return null. I need help for the same. Attaching the classes and property files.
Please note: I have tried different ways from "https://www.baeldung.com/properties-with-spring "How to access a value defined in the application.properties file in Spring Boot".
But this is not helping in getting the value from the application.properties file.
#SpringBootApplication
#EnableAutoConfiguration
#PropertySource("classpath:application.properties")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
SampleTest ap=new SampleTest();
System.out.println("+++++++++++++ "+ap.returnvalue());
}
#Configuration
#PropertySource("classpath:application.properties")
public class SampleTest {
#Value("${testValue}")
String value1;
public String returnvalue()
{
return value1;
}
}
application.properties file
testValue=Value from application
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>ReadValue</groupId>
<artifactId>com.read.vale</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>com.read.vale</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

Try this:
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Application.class, args);
SampleTest ap = ctx.getBean(SampleTest.class);
System.out.println("+++++++++++++ "+ap.returnvalue());
}

Related

Spring boot controller issue, getting whitelabel page error ,what might be wrong here?

I have made projects before on spring and never got any issue like this before ,i just can't seem to figure out what is wrong here, all dependencies are correctly imported .
Here is how my vaultengine.java looks like :
VaultEngine.java :
package com.example.VaultEngine;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#SpringBootApplication
#Controller
public class VaultEngineApplication {
public static void main(String[] args) {
SpringApplication.run(VaultEngineApplication.class, args);
}
#GetMapping("/h")
public String greetingForm() {
return "index.html";
}
}
I am using a custom port cause i have 8080 in use somewhere already.
application.properties:
server.port=8989
All dependencies are properly loaded.
pom.xml file :
<?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 https://maven.apache.org/xsd/maven-
4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<version>1</version>
</configuration>
</plugin>
</plugins>
</build>
</project>
You are using spring-boot-starter-thymeleaf, so
#GetMapping("/h")
public String greetingForm() {
return "index";
}
and put index.html to src/main/resources/templates/.
refs:
https://spring.io/guides/gs/serving-web-content/
https://docs.spring.io/spring-boot/docs/2.2.6.RELEASE/reference/htmlsingle/#boot-features-spring-mvc-template-engines
https://docs.spring.io/spring/docs/5.2.7.RELEASE/spring-framework-reference/web.html#mvc-controller
Use #RestController instead of #Controller
#SpringBootApplication
#RestController
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
#GetMapping("/h")
public String greetingForm() {
return "index.html";
}
}

Spring-boot scheduler runs without #EnableScheduling annotation

I was following this example to create a scheduled task in a sample project: https://spring.io/guides/gs/scheduling-tasks
It says, #EnableScheduling ensures that a background task executor is created. Without it, nothing gets scheduled.
But, by mistake i didn't use it. How come it still works?
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>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<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-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Other Classes:
#SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
#Component
public class ScheduledTaskRunner {
Logger log = LoggerFactory.getLogger(ScheduledTaskRunner.class);
#Scheduled(cron = "0/1 * * * * *")
public void run(){
log.info("Hello");
}
}
Isn't the scheduled task supposed to not run?
Your code added the spring-boot-starter-actuator when that is added a scheduler is added as well. This scheduler is used by the actuator to schedule the automatic export of the metrics.
The code for this is in the MetricExportAutoConfiguration and has been added in Spring Boot 1.3.0.
So if you remove the actuator scheduling won't work anymore.

Disable authentication for some methods

when I include the following dependency Spring asks for authentication for all rest requests.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
my 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>com.mycompany</groupId>
<artifactId>myapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MyApp</name>
<description></description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
My UserController.java
#RestController
#RequestMapping("/users")
public class UserController {
#Autowired
UserService us;
#RequestMapping("/all")
public Hashtable<String, User> getAll() {
return us.getAll();
}
#RequestMapping("{id}")
public User getPerson(#PathVariable("id") String id) {
return us.getPerson(id);
}
#RequestMapping(method = RequestMethod.POST)
public void registerNewUser() {
us.registerNewUser();
}
}
How can I disable authentication for the registerNewUser() method? It's for registering a user and I don't want any authentication for that method.
You need a security configuration class and from there you can explicitly restrict that behaviour. For example:
#Configuration
#EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
#Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/users/**").authenticated()
.antMatchers(HttpMethod.POST, "/users").permitAll();
}
}

SpringBootApplication cannot be resolved to a type

Hi below is my Spring code and pom.xml. Maven is not able to update even though I tried to force project update. Any resolution on this or direction where I am getting wrong.
package com.boot;
import org.springframework.*;
#SpringBootApplication
public class App
{
public static void main( String[] args )
{
System.out.println("Hello World");
}
}
pom
<groupId>com.boot</groupId>
<artifactId>das-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<name>das-boot</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>com.boot.App</start-class>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
I am not getting why Maven is not able to get the dependencies for my project. A
I faced the same issue.
I changed the version from 2.0.5.RELEASE to 2.0.3.RELEASE to make it work
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
If you are fine to move to 2.x then please follow below steps:
Step 1 copy below pom
<?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>com.boot</groupId>
<artifactId>das-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>das-boot</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Step 2: Here is your main application class:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class DasBootApplication {
public static void main(String[] args) {
SpringApplication.run(DasBootApplication.class, args);
}
}
or you can also stay with 1.4.2, just change java version to 1.6.
You should provide the class name in the run() method like below:
#SpringBootApplication
public static void main(String[] args) {
SpringApplication.run(App.class, args);
Sytem.out.print("Started");
}
And remove the below line from pom.xml
<start-class>com.boot.App</start-class>

Getting error running spring boot application using -jar

This question has been asked over and over, however I couldn't find my answer. I have an application using spring boot, which intellij can run it without any issue, however java -jar echohostname.jar give me this error:
Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean
Here is my Main class:
public class ApplicationMain {
public static void main(String[] args) {
SpringApplicationBuilder builder = new SpringApplicationBuilder(SpringConfiguration.class);
ConfigurableApplicationContext applicationContext = builder.run(args);
}
This is my Controller class:
#RestController
public class Controller {
#Autowired
#Qualifier("getSigarProxy")
private SigarProxy sigarProxy;
#RequestMapping(value = "/hostname", method = RequestMethod.GET)
public String hostName() throws SigarException {
return "HELLO THERE";
//return sigarProxy.getNetInfo().getHostName();
}
}
And this is spring configuration class:
#SpringBootApplication
#Configuration
#ComponentScan("controller")
public class SpringConfiguration {
#Bean
public static PropertySourcesPlaceholderConfigurer getPropertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
#Bean
public Sigar getSigar() {
return new Sigar();
}
#Bean
public SigarProxy getSigarProxy() {
return SigarProxyCache.newInstance(getSigar(), 1000);
}
#Bean
#Scope("prototype")
public HttpHeaders getHttpHeaders() {
return new HttpHeaders();
}
#Bean
#Scope("prototype")
public AsyncRestTemplate getRestTemplate() {
return new AsyncRestTemplate();
}
}
POM file:
<?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>echoHostname</groupId>
<artifactId>echoHostname</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.fusesource</groupId>
<artifactId>sigar</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>1.2.3.RELEASE</version>
</dependency>
</dependencies>
</project>
UPDATED MAIN:
public class ApplicationMain {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(SpringConfiguration.class, args);
}
}
UPDATED POM:
<?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>echoHostname</groupId>
<artifactId>echoHostname</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.fusesource</groupId>
<artifactId>sigar</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>1.2.7.RELEASE</version>
</dependency>
</dependencies>
</project>
Any help is appreciated.
Add class level #SpringBootApplication annotation in ApplicationMain.java
Add the below plugin in your pom.xml. Build the maven project and execute the jar command
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
This page has more information on maven assembly plugin - https://maven.apache.org/plugins/maven-assembly-plugin/usage.html
In a spring-boot project, you need that the parent pom be
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
You can remove the spring dependencies, like context, core and beans.
Add to the pom
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
And build the jar again.

Resources