Adding properties for a bean - java

I have following bean class. I want to define this bean into the xml file.
I want to know which objects of this bean are added as a property of the bean in the xml?
public class Mybean{
public String name;
public String address;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name=name;
}
public String getAddress()
{
return address;
}
}

Since you have getters and setters for the name and address fields they can both serve as properties.
<bean id="mybean" class="package.to.MyBean">
<property name="name" value="something"/>
<property name="address" value="something"/>
</bean>
Reference: http://www.springbyexample.org/examples/intro-to-ioc-basic-setter-injection.html

Let your class implement InitializingBean, then in the afterPropertiesSet() method, you can check which property has been set by spring

Related

Invalid property 'name' of bean class [Country]: Bean property 'name' is not writable or has an invalid setter method. Did you mean 'cname'?

I am new to springs, so I was just trying to implement inheritance in spring.
Customer.java
public class Customer {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Country.java
public class Country {
String cname;
String city;
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
Main.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String args[]){
ApplicationContext context = new ClassPathXmlApplicationContext("Bean1.xml");
Customer cus = (Customer) context.getBean("customer");
System.out.println(cus.getName());
Country con = (Country) context.getBean("country");
System.out.println(con.getCname());
}
}
Bean1.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-3.0.xsd">
<bean id = "customer" class = "Customer">
<property name = "name" value = "Garima"/>
</bean>
<bean id ="country" class="Country" parent="customer">
<property name = "cname" value = "India"/>
<property name = "city" value = "Delhi"/>
</bean>
</beans>
Every time I run this without parent in Bean1.xml, this is running fine. As soon as I add parent , I receive the below mentioned error.
Error : Invalid property name of bean class [Country]: Bean property name is not writable or has an invalid setter method. Did you mean cname?
I have noticed this case with many other examples as well.
Can someone please help me with this?
This is because your bean definition suggests that Customer is parent of Country but your class Country doesn't extend Customer
<bean id ="country" class="Country" parent="customer">
So you have two options
Either remove parent="customer" from your bean definition
OR Extend Customer in Country class like
public class Country extends Customer{...

Address value shows null

I have two classes Student and Address which implements IStudent ans IAddress interfaces respectively. Student class has a relationship with Address class. That is why i have declared a reference member of it.
public class Student implements IStudent {
private String code;
private String name;
#Autowired
private IAddress address;
#Override
public String getCode() {
return this.code;
}
#Override
public String getName() {
return this.name;
}
public void setCode(String code) {
this.code = code;
}
public void setName(String name) {
this.name = name;
}
public IAddress getAddress() {
return this.address;
}
public void setAddress(Address address) {
this.address = address;
}
}
and I have Address class
public class Address implements IAddress{
private String city;
private String pinCode;
private String houseNo;
private String roadName;
#Override
public String getCity() {
return this.city;
}
#Override
public String getPinCode() {
return this.pinCode;
}
#Override
public String getHouseNo() {
return this.houseNo;
}
#Override
public String getRoadName() {
return this.roadName;
}
public void setCity(String city) {
this.city = city;
}
public void setPinCode(String pinCode) {
this.pinCode = pinCode;
}
public void setHouseNo(String houseNo) {
this.houseNo = houseNo;
}
public void setRoadName(String roadName) {
this.roadName = roadName;
}
}
In my applicationContext.xml file i have written the following bean definitions
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<bean id="studentbean" class="main.Student">
<property name="code" value="S001"></property>
<property name="name" value="Subhabrata Mondal"></property>
</bean>
<bean id="addressbean" class="main.Address">
<property name="houseNo" value="119/2"></property>
<property name="roadName" value="South Avenue"></property>
<property name="city" value="Delhi"></property>
<property name="pinCode" value="110005"></property>
</bean>
</beans>
When i have checked Student object after initialization of bean, name and code has assigned with setter method. But the address is not assigned. Thus it shows null value for address. I have marked address with #Autowired annotation. Can you please help?
ApplicationContext factory = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student) factory.getBean("studentbean");
System.out.println(student.getAddress());
you need to explicitly wire to Address not IAddress since the CI only knows Address, if you want to wired
#Autowired
private Address address;
or you need to define a bean with type IAddress but make sure you do not have more than implementation or spring will get confused, if you have more than one implementation use can qualifiers to clear the ambiguity
This whole example is kind of strange but you can get rid of the #Autowired annotation and use following bean configuration instead;
<bean id="studentbean" class="main.Student">
<property name="code" value="S001"></property>
<property name="name" value="Subhabrata Mondal"></property>
<property name="address" >
<ref local="addressbean"/>
</property>
</bean>

Exception with Autowired

In autowiring byType if the property type is matched with more than one bean then it would throw an exception, but I can't see any exception when I am using the annotation #Autowired and defined two beans with same property type.
Below is the code:
Employee.java:
public class Employee {
private int id;
private String name;
private int salary;
// Getter and Setter
}
Dept:
public class Dept {
#Autowired
private Employee emp;
public Employee getEmp() {
return emp;
}
public void setEmp(Employee emp) {
this.emp = emp;
}
#Override
public String toString() {
return emp.getName();
}
}
Beans.xml:
<bean id = "dept" class = "Dept"></bean>
<bean id = "emp" class = "Employee">
<property name="id" value="25"></property>
<property name="name" value="Ram"></property>
<property name="salary" value="32000"></property>
</bean>
<bean id = "emp1" class = "Employee">
<property name="id" value="25"></property>
<property name="name" value="Sanju"></property>
<property name="salary" value="32000"></property>
</bean>
AppMain.java:
public class AppMain {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
Dept d = (Dept)context.getBean("dept");
System.out.println(d);
}
}
Please correct me if I am doing any thing wrong in it.
Spring is matching the emp variable name; if your beans were emp1 and emp2 you'd get an exception (unless you add a #Qualifier to the #AutoWired field).
You have defined a variable named as "emp" of Employee class which is same as the bean with id as "emp".Because of this spring don't get confused understanding which bean it has to inject.and if you change the bean id from "emp" to something else you will get an unsatisfied bean dependency exception. read more here

No constructor with 3 arguments defined in class?

Let me clear it, I'm a completely beginner in Spring framework.
I've three class files, Now i'm getting an error into beans.xml. You could take a look into my codes.
Here is MyAddress.java:
package com.project;
public class MyAddress {
private String city;
private String state;
private String address;
public void Address(String city, String state, String address){
this.city=city;
this.state=state;
this.address=address;
}
public String toString(){
return city+" "+state+" "+address;
}
}
Here is my Employee.java
package com.project;
public class Employee {
private int id;
private String name;
private MyAddress address;
public Employee(){
System.out.print("Default constructor..");
}
public void Employee(int id, String name, MyAddress address){
this.id=id;
this.name=name;
this.address=address;
}
public void show(){
System.out.println(id+" "+name);
System.out.println(address.toString());
}
}
Here is my MainProgram.java
package com.project;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainProgram {
public static void main(String[] args){
ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
Employee em=(Employee)ac.getBean("e");
em.show();
}
}
and finally here is my 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="e" class="com.project.MyAddress">
<constructor-arg value="USA" type="String"></constructor-arg>
<constructor-arg value="Delhi" type="String"></constructor-arg>
<constructor-arg value="Bangalore" type="String"></constructor-arg>
</bean>
<bean id="e2" class="com.project.Employee">
<constructor-arg value="123" type="int"></constructor-arg>
<constructor-arg value="raj"></constructor-arg>
<constructor-arg>
<ref bean="e"/>
</constructor-arg>
</bean>
</beans>
I'm getting an error in beans.xml files as No constructor with 3 arguments defined in class
PLease help, what's that mean?
Surely, help would be appreciated!!
This
public void Address(String city, String state, String address)
should be
public MyAddress(String city, String state, String address)
You got the class name wrong in your constructor, and in addition, constructors don't have a return type.
You have a similar error for Employee :
public void Employee(int id, String name, MyAddress address)
should be
public Employee(int id, String name, MyAddress address)
As defined here,
"A class contains constructors that are invoked to create objects from the class blueprint. Constructor declarations look like method declarations—except that they use the name of the class and have no return type"
In the MyAddress class instead of creating a constructor you created a Address method,
changing public void Address(...) to public MyAddress(...) will make it work
The address class has a default constructor. Omit void keyword from method.

How to make IReport access a get method stack on a Javabean datasource

I have a Bean with some properties that is a reference for another Bean and I would like to show in my report some property of the property of the bean through its corresponding get method.
For example:
class Person {
private Address ad;
public Address getAddress() {
return this.ad;
}
}
class Address {
private String city;
public String getCity() {
return this.city
}
}
I would like pass as a DataSource for the report a Collection of Person and I would like to create a field in the report to show the city of the address of each Person in the collection.
You can declare fields like this:
<field name="city" class="java.lang.String">
<fieldDescription><![CDATA[person.address.city]]></fieldDescription>
</field>

Categories