Sunteți pe pagina 1din 10

Jspiders-Spring/SESM-13/Spring-Annotation-basics/src/com/spring/basic/Person.

java
JoshiKshitij annotation example
7e89f21 6 hours ago
14 lines (8 sloc) 197 Bytes

package com.spring.basic;

import org.springframework.stereotype.Component;

@Component
public class Person {

public Person() {
System.out.println("--- Person Object Created ---");
}

package com.spring.basic;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Runner {

public static void main(String[] args) {


ApplicationContext container =
new ClassPathXmlApplicationContext("Spring-core.xml");
System.out.println("container created");

Person person = container.getBean( "person",Person.class);

}
<context:component-scan base-package="com" />

Jspiders-Spring/SESM-13/Spring-Anotation-
AutoWiring/src/com/spring/demo/MyDemoApp.java
JoshiKshitij autowire
8985cb4 5 hours ago
21 lines (15 sloc) 420 Bytes

package com.spring.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

// meta info for the container that please create


// an object for this particular object
// of this Class with you
@Component
// MyDemoApp myDemoApp = new MyDemoApp();
public class MyDemoApp {

@Autowired
MySqlDao dao;

public void saveObject() {


dao.saveObject();
}

Jspiders-Spring/SESM-13/Spring-Anotation-
AutoWiring/src/com/spring/demo/MySqlDao.java
JoshiKshitij autowire
8985cb4 5 hours ago
13 lines (8 sloc) 212 Bytes

package com.spring.demo;

import org.springframework.stereotype.Component;

@Component
public class MySqlDao {

public void saveObject(){


System.out.println("Object saved using MYSQL Implimenation");
}

Jspiders-Spring/SESM-13/Spring-Anotation-AutoWiring/src/com/spring/demo/Runner.java
JoshiKshitij autowire
8985cb4 5 hours ago
19 lines (11 sloc) 437 Bytes

package com.spring.demo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Runner {

public static void main(String[] args) {

ApplicationContext contianer = new


ClassPathXmlApplicationContext("context.xml");
System.out.println("container created");

MyDemoApp app = contianer.getBean(MyDemoApp.class);


app.saveObject();

<context:component-scan
base-package="com.spring.demo"/>

Jspiders-Spring/SESM-13/Spring-Anotation-basics2/src/com/spring/demo/MyDemoApp.java
JoshiKshitij autowire
8985cb4 5 hours ago
19 lines (15 sloc) 375 Bytes

package com.spring.demo;

import org.springframework.stereotype.Component;

// meta info for the container that please create


// an object for this particular object
// of this Class with you
@Component
// MyDemoApp myDemoApp = new MyDemoApp();
public class MyDemoApp {

public MyDemoApp() {
System.out
.println
(" --- MyDemoApp Object Created --- ");
;
}

Jspiders-Spring/SESM-13/Spring-Anotation-
basics2/src/com/spring/demo/MySqlDataBaseDao.java
JoshiKshitij autowire
8985cb4 5 hours ago
14 lines (9 sloc) 230 Bytes

package com.spring.demo;

import org.springframework.stereotype.Component;

@Component
public class MySqlDataBaseDao {

public MySqlDataBaseDao() {
System.out.println
(" --- MySqlDataBaseDao Object Created --- " );
}

JoshiKshitij autowire
8985cb4 5 hours ago
16 lines (9 sloc) 360 Bytes

package com.spring.demo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Runner {

public static void main(String[] args) {

ApplicationContext contianer = new


ClassPathXmlApplicationContext("context.xml");
System.out.println("container created");

<context:component-scan
base-package="com.spring.demo"/>

Jspiders-Spring/SESM-13/Spring-BeanLifeCycle/src/com/spring/demo/Human.java
JoshiKshitij lide cycle
52ae660 3 days ago
21 lines (14 sloc) 320 Bytes

package com.spring.demo;

public class Human {

public Human() {
System.out.println("Object instantiation phase");
}

// init method
public void giveName(){
System.out.println("giving name / init phase");
}

// destory method
public void death() {
System.out.println("death / destroy phase");
}

Jspiders-Spring/SESM-13/Spring-BeanLifeCycle/src/com/spring/demo/Runner.java
JoshiKshitij lide cycle
52ae660 3 days ago
20 lines (14 sloc) 606 Bytes

package com.spring.demo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Runner {

public static void main(String[] args) {


System.out.println("container created");
AbstractApplicationContext contianer = new
ClassPathXmlApplicationContext("context.xml");

Human ram = contianer.getBean(Human.class);


System.out.println("contianeris being destroyed");
contianer.close();
System.out.println("contianer destroyed");
}

<bean id="ram" class="com.spring.demo.Human" init-method="giveName" destroy-


method="death"></bean>

Jspiders-Spring/SESM-13/Spring-Di-Autowireing/src/com/spring/demo/Husband.java
JoshiKshitij autowiring
7c993c7 4 days ago
33 lines (24 sloc) 469 Bytes

package com.spring.demo;

public class Husband {

private String name;


private Wife wife;

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public Wife getWife() {


return wife;
}

public void setWife(Wife wife) {


this.wife = wife;
}

@Override
public String toString() {
return "Husband [name=" + name + ", wife=" + wife + "]";
}

public Husband() {
System.out.println("Husband create");
}

}
Jspiders-Spring/SESM-13/Spring-Di-Autowireing/src/com/spring/demo/Wife.java
JoshiKshitij autowiring
7c993c7 4 days ago
24 lines (17 sloc) 317 Bytes

package com.spring.demo;

public class Wife {

private String name;


public String getName() {
return name;
}

public void setName(String name) {


this.name = name;
}

@Override
public String toString() {
return "Wife [name=" + name + "]";
}

public Wife() {
System.out.println("Wife created");
}

}
Jspiders-Spring/SESM-13/Spring-Di-Autowireing/src/com/spring/demo/Runner.java
JoshiKshitij autowiring
7c993c7 4 days ago
20 lines (13 sloc) 546 Bytes

package com.spring.demo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Runner {

public static void main(String[] args) {

ApplicationContext contianer = new


ClassPathXmlApplicationContext("context.xml");
System.out.println("container created");

Husband kamlesh = contianer.getBean("kamlesh", Husband.class);


System.out.println(kamlesh);

Husband akash = contianer.getBean("akash", Husband.class);


System.out.println(akash);
}

}
<bean id="kamlesh" class="com.spring.demo.Husband"
autowire="byType">
<property name="name" value="Kamlesh"></property>
</bean>

<bean id="akash" class="com.spring.demo.Husband"


autowire="byType">
<property name="name" value="akash"></property>
</bean>

<bean id="joyti" class="com.spring.demo.Wife">


<property name="name" value="Joyti"></property>
</bean>

Jspiders-Spring/SESM-13/Spring-Di-Autowireing2/src/com/spring/demo/Husband.java
JoshiKshitij lide cycle
52ae660 3 days ago
33 lines (24 sloc) 469 Bytes

package com.spring.demo;

public class Husband {

private String name;


private Wife wife;

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public Wife getWife() {


return wife;
}

public void setWife(Wife wife) {


this.wife = wife;
}

@Override
public String toString() {
return "Husband [name=" + name + ", wife=" + wife + "]";
}

public Husband() {
System.out.println("Husband create");
}

Jspiders-Spring/SESM-13/Spring-Di-Autowireing2/src/com/spring/demo/Wife.java
JoshiKshitij lide cycle
52ae660 3 days ago
24 lines (17 sloc) 317 Bytes

package com.spring.demo;

public class Wife {

private String name;

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

@Override
public String toString() {
return "Wife [name=" + name + "]";
}

public Wife() {
System.out.println("Wife created");
}

Jspiders-Spring/SESM-13/Spring-Di-Autowireing2/src/com/spring/demo/Runner.java
JoshiKshitij lide cycle
52ae660 3 days ago
21 lines (15 sloc) 560 Bytes

package com.spring.demo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Runner {

public static void main(String[] args) {

ApplicationContext contianer = new


ClassPathXmlApplicationContext("context.xml");
System.out.println("container created");

Husband kamlesh = contianer.getBean("kamlesh", Husband.class);


System.out.println(kamlesh);
/*
* Husband akash = contianer.getBean("akash", Husband.class);
* System.out.println(akash);
*/
}

<!-- autowire="byName" -->


<!-- it will llok for the ref of bean and matchs with the ref rest
inside the class
if ref matchs it will do the DI else will Di will not be
done-->
<bean id="kamlesh" class="com.spring.demo.Husband"
autowire="byName" >
<property name="name" value="Kamlesh"></property>
</bean>

<bean id="joyti" class="com.spring.demo.Wife">


<property name="name" value="Joyti"></property>
</bean>

<bean id="wife" class="com.spring.demo.Wife">


<property name="name" value="deepika"></property>
</bean>

Jspiders-Spring/SESM-13/Spring-Di-Autowireing3/src/com/spring/demo/Husband.java
JoshiKshitij lide cycle
52ae660 3 days ago
26 lines (17 sloc) 418 Bytes

package com.spring.demo;

public class Husband {

private String name;


private Wife wife;

public Husband() {
System.out.println("Husband create");
}

public Husband(String name, Wife wife) {


System.out.println("---- para constructor of husband ----");
this.name = name;
this.wife = wife;
}

@Override
public String toString() {
return "Husband [name=" + name + ", wife=" + wife + "]";
}

Jspiders-Spring/SESM-13/Spring-Di-Autowireing3/src/com/spring/demo/Wife.java
JoshiKshitij lide cycle
52ae660 3 days ago
24 lines (17 sloc) 317 Bytes

package com.spring.demo;

public class Wife {

private String name;

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

@Override
public String toString() {
return "Wife [name=" + name + "]";
}

public Wife() {
System.out.println("Wife created");
}

}
Jspiders-Spring/SESM-13/Spring-Di-Autowireing3/src/com/spring/demo/Runner.java
JoshiKshitij lide cycle
52ae660 3 days ago
18 lines (11 sloc) 456 Bytes

package com.spring.demo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Runner {

public static void main(String[] args) {

ApplicationContext contianer = new


ClassPathXmlApplicationContext("context.xml");
System.out.println("container created");

Husband kamlesh = contianer.getBean("kamlesh", Husband.class);


System.out.println(kamlesh);

<!-- autowire="constructor" -->


<!-- it is same as autowire byType first and if it fails its is done by
byName
,only difference is
here constructor injection will be used
-->
<bean id="kamlesh" class="com.spring.demo.Husband"
autowire="constructor">
<constructor-arg type="String" value="Kamlesh"></constructor-arg>

</bean>

<bean id="joyti" class="com.spring.demo.Wife">


<property name="name" value="Joyti"></property>
</bean>

<bean id="deepika" class="com.spring.demo.Wife">


<property name="name" value="deepika"></property>
</bean>

S-ar putea să vă placă și