UM1979
UM1979

Reputation: 162

Spring, XMLCOnfiguration: Without indicating annotation in xml file how it is working?

I am trying to write a spring program with using Annotation config in xml file, It is working without this configuration initiating also. I wrote method() in one file and and declared CLassPathXmlApplicationContext with Beans.xml file, I wrote a Student bean and applied the @Required annotation for setter methods, Please find the Bean configuration xml file 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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:annotation-config/>

<!-- Definition for student bean -->
<bean id = "student" class = "com.tutorialspoint.Student">
  <property name = "name" value = "Zara" />
  <property name = "age"  value = "11"/>
</bean>

</beans>

When i run this program with or without following Annotation indication in xml file, the code works fine, Please guide me the underlying reason for it.

<context:annotation-config/>

Upvotes: 0

Views: 73

Answers (1)

TuyenNTA
TuyenNTA

Reputation: 1204

The @Required annotation applies to bean property setter methods and it indicates that the affected bean property must be populated in XML configuration file at configuration time.

Once <context:annotation-config/> is configured, you can start annotating your code to indicate that Spring should automatically wire values into properties, methods, and constructors. It does not means that some things will wrong without it when you using the @Required

In your xml, you have defined your properties, so the @Required will run well

Upvotes: 1

Related Questions