The @Required annotation

The @Required annotation is applied to bean property-setter functions. The bean property must be populated in the XML configuration file at configuration-time. This annotation essentially shows that the setter function must be arranged to be dependency-injected with a value at configuration-time.

Add a user model and the Main class with a bean.xml configuration file.

The content of the bean.xml configuration file is as follows:

<?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.xsd">

<context:annotation-config/> <!--after this tag, we have to write the beans-->

<bean id="users" class="requiredAnnotation.UsersForReq">
<property name="name" value="Naruto Uzumaki"/>
<property name="village" value="Konohagakure"
/>
</bean>
</beans>

The content of UsersForReq.kt is as follows:

class Users{
private var village: String? = null
private var name: String? = null

@Required
fun setVillage(village: String?) {
this.village = village
}

fun getVillage(): String? {
return village
}

@Required
fun setName(name: String) {
this.name = name
}

fun getName(): String? {
return name
}
}

The content of AnnotationBasedReqApp.kt is as follows:

fun main(args: Array<String>) {
val context = ClassPathXmlApplicationContext("requiredAnnotation/beans_for_req.xml")
val users = context.getBean("users") as UsersForReq

println("Name: "+users.getName())
println("Village: "+users.getVillage())
}

The output of this project will be as follows:

Name: Naruto Uzumaki
Village: Konohagakure
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.0.85