Bean getting injected without using #Autowired - java

I was doing some #Autowired annotations in my code where I stumbled upon a doubt. I am getting the name as John even though I have not injected the Employee bean in GetName.java. The bean was getting injected even though I was not autowiring it using any annotation. Is there some specific prerequisite need to be kept in mind while using constructors ?
Employee.Java
package com.sample.employee;
public class Employee {
public String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
GetName.java
package com.sample.salary;
import org.springframework.beans.factory.annotation.Autowired;
import com.sample.employee.Employee;
public class GetName {
// #Autowired
public Employee emp;
// #Autowired
public GetName(Employee emp) {
this.emp = emp;
}
public void displayName() {
System.out.println(emp.getName()); //prints John
}
}
spring.xml
<?xml
version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
xmlns:context="http://www.springframework.org/schema/context">
<context:annotation-config />
<!-- <context:component-scan base-package="com.sample.employee"/> -->
<bean id="empl" class="com.sample.employee.Employee">
<property name="name" value="John" />
</bean>
<bean id="GetNames" class="com.sample.salary.GetName"/>
</beans>
MainClass.java
package com.sample.employee;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.sample.salary.SalaryCalculator;
public class MainCLass {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
GetName cal = (GetName) context.getBean("GetNames");
cal.displayName();
}
}
Am I missing out something?

If there is only one Constructor for a Bean, Spring will do constructor injection and the #Autowired annotation can be omitted.
See https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-spring-beans-and-dependency-injection.html
This feature was added with one of the more recent versions.

Related

Changes from spring mvc not getting reflected in oracle database

I am learning to establish connection to oracle database from Spring MVC . so here is what i have done so far
spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<context:component-scan base-package="org.springdemo" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:#192.168.0.8:1521:xe"/>
<property name="username" value="sys as sysdba"/>
<property name="password" value="7299"/>
</bean>
</beans>
Circle.java
package org.springdemo.model;
public class Circle {
private int id;
private String name;
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 Circle(int id , String name){
setId(id);
setName(name);
}
}
JdbcDaoImpl.java
package org.springdemo.dao;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
#Component
public class JdbcDaoImpl {
#Autowired
private DataSource dataSource;
private JdbcTemplate jdbcTemplate = new JdbcTemplate();
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void getCircle(){
String sql = "SELECT COUNT(*) FROM circle";
jdbcTemplate.setDataSource(getDataSource());
int count = jdbcTemplate.queryForObject(sql,Integer.class);
System.out.println("count is"+count);
}
public void delete(int id) {
JdbcTemplate delete = new JdbcTemplate(dataSource);
delete.update("DELETE from CIRCLE where ID= ?",
new Object[] {id});
System.out.println("deleted successfully");
}
}
JdbcDemo.java
package org.springdemo;
import org.springdemo.dao.JdbcDaoImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class JdbcDemo {
public static void main(String[] args) {
ApplicationContext ctxt = new ClassPathXmlApplicationContext("spring.xml");
JdbcDaoImpl circleDao =(JdbcDaoImpl) ctxt.getBean("jdbcDaoImpl", JdbcDaoImpl.class);
circleDao.getCircle();
circleDao.delete(2);
}
}
Everything seems to be working fine without any errors and i am getting following output.
but in database i have 3 rows and my table is not getting updated .
when i tried to query for a non existing table to confirm whether i am connected to DB , i am getting exception indicating that i am connected to database only . Can someone explain me what would be the possible reason for my changes not reflecting in database.
Let's Spring instantiate your jdbctemplate
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
#Autowired
private JdbcTemplate jdbcTemplate
Don't use Obect[] but only id.
What is returned by the method delete.update ?

UnsatisfiedDependencyException: Error creating bean with name "empController"

I want to learn Spring, so I write simple java CRUD app. But from the begining have errors org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'empController'" and org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dao' for my servlet. I was looking some solutions, but nothing works.
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>Employer</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Employer</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Employer-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.javatpoint"></context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/Employers"></property>
<property name="username" value="root"></property>
</bean>
<bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds"></property>
</bean>
<bean id="dao" class="com.javatpoint.EmpDao">
<property name="jdbcTemplate" ref="jt"></property>
</bean>
</beans>
EmpController.java
package com.javatpoint;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class EmpController {
#Autowired
EmpDao empDao;
#RequestMapping("/empform")
public ModelAndView show() {
return new ModelAndView("empform", "command", new Emp());
}
#RequestMapping(value="/save", method=RequestMethod.POST)
public ModelAndView save(#ModelAttribute("emp")Emp emp) {
empDao.save(emp);
return new ModelAndView("redirect:/viewemp");
}
#RequestMapping("/viewemp")
public ModelAndView viewemp() {
List<Emp> list = empDao.getEmployees();
return new ModelAndView("viewemp", "list", list);
}
#RequestMapping(value="/editemp/{id}")
public ModelAndView edit(#PathVariable("id")int id) {
Emp emp = empDao.getById(id);
return new ModelAndView("empeditform", "command", emp);
}
#RequestMapping(value="editsave", method=RequestMethod.POST)
public ModelAndView editsave(#ModelAttribute("emp")Emp emp) {
empDao.update(emp);
return new ModelAndView("redirect:/viewemp");
}
#RequestMapping("/delete")
public ModelAndView delete(#ModelAttribute("emp")Emp emp) {
empDao.delete(emp);
return new ModelAndView("redirect:/viewemp");
}
}
EmpDao.java
package com.javatpoint;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
public class EmpDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public int save(Emp emp) {
String sql = "insert into Employers values('"+emp.getId()+"','"+emp.getName()+"','"+emp.getSalary()+"','"+emp.getDesignation()+"')";
return jdbcTemplate.update(sql);
}
public int update(Emp emp) {
String sql = "update Employers set name='"+emp.getName()+"',salary='"+emp.getSalary()+"',designation='"+emp.getDesignation()+"' where id='"+emp.getId()+"'";
return jdbcTemplate.update(sql);
}
public int delete(Emp emp) {
String sql = "delete from Employers where id='"+emp.getId()+"'";
return jdbcTemplate.update(sql);
}
public Emp getById(int id) {
String sql = "select * form Employers where id=?";
return jdbcTemplate.queryForObject(sql, new Object[] {id}, new BeanPropertyRowMapper<Emp>(Emp.class));
}
public List<Emp> getEmployees(){
return jdbcTemplate.query("select * from Employers", new RowMapper<Emp>() {
public Emp mapRow(ResultSet rs, int row) throws SQLException{
Emp emp = new Emp();
emp.setId(rs.getInt(1));
emp.setName(rs.getString(2));
emp.setSalary(rs.getFloat(3));
emp.setDesignation(rs.getString(4));
return emp;
}
});
}
}
Emp.java
package com.javatpoint;
public class Emp {
private int id;
private String name;
private float salary;
private String designation;
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 float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
}
The main problem is that you're trying to autowire an EmpDao to your controller without havin an EmpDao bean.
In order to make EmpDao a bean you should annotate the EmpDao class with #Component , #Service or #Repository:
#Service
public class EmpDao {
#Autowired
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public int save(Emp emp) {
String sql = "insert into Employers values('"+emp.getId()+"','"+emp.getName()+"','"+emp.getSalary()+"','"+emp.getDesignation()+"')";
return jdbcTemplate.update(sql);
}
public int update(Emp emp) {
String sql = "update Employers set name='"+emp.getName()+"',salary='"+emp.getSalary()+"',designation='"+emp.getDesignation()+"' where id='"+emp.getId()+"'";
return jdbcTemplate.update(sql);
}
public int delete(Emp emp) {
String sql = "delete from Employers where id='"+emp.getId()+"'";
return jdbcTemplate.update(sql);
}
public Emp getById(int id) {
String sql = "select * form Employers where id=?";
return jdbcTemplate.queryForObject(sql, new Object[] {id}, new BeanPropertyRowMapper<Emp>(Emp.class));
}
public List<Emp> getEmployees(){
return jdbcTemplate.query("select * from Employers", new RowMapper<Emp>() {
public Emp mapRow(ResultSet rs, int row) throws SQLException{
Emp emp = new Emp();
emp.setId(rs.getInt(1));
emp.setName(rs.getString(2));
emp.setSalary(rs.getFloat(3));
emp.setDesignation(rs.getString(4));
return emp;
}
});
}
}
Remember, you can only inject (or autowire) other beans into your beans.
In spring, the application creates it's own beans (either as singletons, or a 'new' instance anytime you need it.)
EmpController for example is annotated with #Controller to tell spring exactly that- this is a controller, as the application starts up, please create a new instance of this bean, so I can use it.
However, as spring tries to create this bean, it also populates it's variables.
#Autowired on the empDao variable means roughly: "you should already have built an instance of the class empDao, so please, let me have here a reference to it(empDao) so I can call it from this class(empController)"
#Controller
public class EmpController {
#Autowired
EmpDao empDao;
But it seems empDao itself is not configured properly- no annotation to let spring know it should create an instance of it while starting up.
try the following 2 changes:
#Service
public class EmpDao {
and
#Entity
public class Emp {

Autowired Annotation not working when uisng <context:annotation-config /> in ApplicationContext.xml

<context:annotation-config />
when above line is added in ApplicationContext.xml file the Test.java is not running but when i add this it works but in the tutorial i was going through it says both the methods work can someone help whats the problem with
above line of code.
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
Employee.java
package com.stp.beans;
import org.springframework.beans.factory.annotation.Autowired;
public class Employee {
private Address empaddress;
public Address getEmpaddress() {
return empaddress;
}
#Autowired
public void setEmpaddress(Address empaddress) {
this.empaddress = empaddress;
}
}
Address.java
package com.stp.beans;
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean id ="empbean" class="com.stp.beans.Employee"></bean>
<bean id ="adrbean" class="com.stp.beans.Address"></bean>
</beans>
Test.java
package com.stp.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.stp.beans.Employee;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("/com/stp/cfgs/ApplicationContext.xml");
Employee emp = (Employee) context.getBean("empbean");
System.out.println(emp.getEmpaddress());
}
}
When you use <context:annotation-config />, your ApplicationContext.xml should be like below.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:annotation-config />
<bean id ="empbean" class="com.stp.beans.Employee"></bean>
<bean id ="adrbean" class="com.stp.beans.Address"></bean>
</beans>
You had just missed the context related definitions.

Why After Advice is getting print before method call

MY Spring AOP programm is not working as Expected.
I created simple AOP annotation program below but the output is not what I thought.
Programm :
Beans.xml
<?xml version = "1.0" encoding = "UTF-8"?>
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop = "http://www.springframework.org/schema/aop"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<aop:aspectj-autoproxy/>
<bean id="student" class="com.surajhome.practice.spring.Student" >
<property name="name" value="Suraj Kudale"></property>
<property name="age" value="27"></property>
</bean>
<bean id="logging" class="com.surajhome.practice.spring.Logging"></bean>
</beans>
Student.java
package com.surajhome.practice.spring;
public class Student {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
String name;
int age;
}
Logging.Java
package com.surajhome.practice.spring;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
#Aspect
public class Logging {
#Pointcut("execution(* com.surajhome.practice.spring.*.*(..) )")
public void selectAll()
{
}
#After("selectAll()")
public void afterAdvice()
{
System.out.println("After Advice called");
}
#Before("selectAll()")
public void beforeAdvice()
{
System.out.println("Before Advice called");
}
public void afterReturningAdvice()
{
System.out.println("After Returning Advice called");
}
public void afterThrowingException()
{
System.out.println("After Exception Advice called");
}
}
MainApp.java
package com.surajhome.practice.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext appContext=new ClassPathXmlApplicationContext("Beans.xml");
Student std=(Student) appContext.getBean("student");
System.out.println(std.getName());
System.out.println(std.getAge());
}
}
output :
Before Advice called
After Advice called
Suraj Kudale
Before Advice called
After Advice called
27
It should be :
Before Advice called
Suraj Kudale
After Advice called
Before Advice called
27
After Advice called
Think of the flow that happens when you call the System.out.println(std.getName());, first #Before method of the get name is called then get name returns a value and #After is called only then System.out.println gets the string and prints it

#Autowired Annotation in Spring give me NullPointerException

I am trying to use #Autowired Annotation in Spring for Dependency Injection
through a simple program but i give me following error
Exception in thread "main" java.lang.NullPointerException
at Customer.d(Customer.java:8)
at Main.main(Main.java:12)
Through xml configuration it give me correct result.
My xml file
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean id="person" class="Person">
<property name="name" value="khan"/>
</bean>
<bean id="cust" class="Customer"></bean>
</beans>
Customer Class
public class Customer {
#Autowired
private Person p;
public void display(){
System.out.println(p.getName());
}
}
Person Class
public class Person {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private String name;
}
Main Class
public class Main {
public static void main(String[] a) {
Resource r=new ClassPathResource("SpringXml.xml");
BeanFactory factory=new XmlBeanFactory(r);
Customer c=(Customer)factory.getBean("cust");
c.display();
}
}
Try like this
public static void main(String[] args) throws Exception {
ApplicationContext context= new ClassPathXmlApplicationContext("SpringXml.xml");
Customer c = (Customer) context.getBean("cust");
c.display();
}
try this :
<bean id="person" class="com.yourpackage.Person">
<property name="name" value="khan"/>
</bean>
<bean id="cust" class="com.yourpackage.Customer">
<property name="p" ref="person"/>
</bean>
dont forget to add your fullpath class package

Categories