Java spring project part 3

Quality assurance, Devs and SEO teams ready to help your business grow

Java spring project part 3

Then use the bean tag to define a bean. It requires at least the class that defines a bean.

In our project we will use the package where we defined a Circle and a Point.

Add a new bean tag in the appContext.xml file

<bean id=”point1″ class=”SpringProject1.beans.Point”>

<constructor-arg name = “x” value=”10″ />

<constructor-arg name = “y” value=”20″ />

</bean>

In order to be able to reffer to this bean from another, we will use an identifier if we want to inject it by name (or we need get bean to obtain that instance)

We can inject it by type.

<constructor-arg value=”10″ /> used to inject a value when the attribute type is a primitive or String.

We can use name to also refer to the variable name.

If we don t use the name the parameters will be taken in the same order that will be written.

<bean id=”circle1″ class=”SpringProject1.beans.Point”>

<constructor-arg name =”ray” value=”10″/>

<constructor-arg name =”center” ref=”point1″/>

</bean>

When using a parameter which is a reference to an object, this will be marked with ref instead of value (which is for Java Primitives and String)

In the Main class we’ll send as parameter the xml configuration file path

Get the absolute path for the appContext.xml file

BeYhzbiBIXaC32Ogx3CCSuB0sNCJLTxLlw4WCnDqJFh69-gUsIG8ofSqQk_w3pt1lGz--tuOMJ6z7AjZ4F87_sQnWnbUDHH9w5qfGJHvquEb8msrCxmtgRB-SztQCpFHIyFETUIp

Put this path as a parameter for the constructor of new FileSystemXmlApplicationContext in Main class

 

No Comments

Add your comment