Skip to content

Commit 41af0ef

Browse files
committed
Added spring bean and scopes
1 parent 6b624c7 commit 41af0ef

File tree

18 files changed

+99
-8
lines changed

18 files changed

+99
-8
lines changed

springapp/cbook.jar

2.01 KB
Binary file not shown.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.cj.pj;
2+
3+
import org.springframework.stereotype.Service;
4+
5+
@Service
6+
public class IBMPrinter implements Printer {
7+
8+
@Override
9+
public void printJob(String data) {
10+
// TODO Auto-generated method stub
11+
12+
}
13+
14+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.cj.pj;
2+
3+
public interface Printer {
4+
5+
public void printJob(String data);
6+
}

springapp/src/main/java/com/lwl/springapp/AppConfiguration.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
import org.springframework.context.annotation.Bean;
44
import org.springframework.context.annotation.Configuration;
55

6-
//@Configuration
6+
@Configuration
77
public class AppConfiguration {
88

99
@Bean
1010
public Greetings greetings() {
1111
BirthdayGreetings obj = new BirthdayGreetings();
1212
obj.setMessage("Happy Birthday!....");
13-
obj.setSender(sender());
13+
obj.setSender(senderObject());
1414
return obj;
1515
}
1616

17-
@Bean
18-
public Sender sender() {
17+
@Bean(name = "senderObj")
18+
public Sender senderObject() {
1919
return new Sender();
2020
}
2121
}

springapp/src/main/java/com/lwl/springapp/BirthdayGreetings.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import org.springframework.beans.factory.annotation.Autowired;
44
import org.springframework.stereotype.Component;
55

6-
import lombok.Getter;
76
import lombok.Setter;
87

98

springapp/src/main/java/com/lwl/springapp/Manager.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ public static void main(String[] args) {
2424

2525

2626
ApplicationContext context = new AnnotationConfigApplicationContext(Manager.class);
27-
Greetings greetings = context.getBean(Greetings.class);
28-
greetings.sendMessage();
27+
String[] names = context.getBeanDefinitionNames();
28+
for(String name:names) {
29+
System.out.println(name);
30+
}
2931
}
3032

3133

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.lwl.springapp.cbook;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.context.ApplicationContext;
5+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
6+
import org.springframework.stereotype.Component;
7+
8+
import com.cj.pj.Printer;
9+
import com.ljava.cbook.service.ContactService;
10+
11+
@Component
12+
public class CbookController {
13+
14+
15+
private final ContactService service;
16+
private final Printer printer;
17+
18+
@Autowired
19+
public CbookController(ContactService service, Printer printer) {
20+
super();
21+
this.service = service;
22+
this.printer = printer;
23+
}
24+
25+
public static void main(String[] args) {
26+
27+
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
28+
CbookController obj = context.getBean(CbookController.class);
29+
System.out.println(context.getBean(ContactService.class));
30+
System.out.println(context.getBean(ContactService.class));
31+
System.out.println(context.getBean(ContactService.class));
32+
33+
}
34+
35+
private void addContact() {
36+
System.out.println(service);
37+
System.out.println(printer);
38+
39+
}
40+
public void viewContacts() {
41+
42+
}
43+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.lwl.springapp.cbook;
2+
import java.lang.annotation.ElementType;
3+
import java.lang.annotation.Retention;
4+
import java.lang.annotation.RetentionPolicy;
5+
import java.lang.annotation.Target;
6+
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.ComponentScan;
9+
import org.springframework.context.annotation.Configuration;
10+
import org.springframework.context.annotation.Scope;
11+
12+
import com.ljava.cbook.service.ContactService;
13+
import com.ljava.cbook.service.ContactServiceImpl;
14+
15+
16+
17+
@Configuration
18+
@ComponentScan(basePackages = {"com.lwl.springapp.cbook","com.cj.pj"})
19+
public class Config {
20+
21+
@Bean
22+
@Scope(value = "singleton")
23+
public ContactService contactService() {
24+
return new ContactServiceImpl();
25+
}
26+
27+
}

springapp/target/classes/META-INF/maven/com.lwl/springapp/pom.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#Generated by Maven Integration for Eclipse
2-
#Fri Jul 10 09:08:58 IST 2020
2+
#Sat Jul 11 08:25:54 IST 2020
33
version=0.0.1-SNAPSHOT
44
groupId=com.lwl
55
m2e.projectName=springapp
513 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)