Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f28fafa

Browse files
Configurar Spring Beans
Tutorial Spring que trata sobre la configuración de los beans y en contenedor IoC.
1 parent 6de875b commit f28fafa

File tree

8 files changed

+161
-0
lines changed

8 files changed

+161
-0
lines changed

‎tutorial_beans/nb-configuration.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project-shared-configuration>
3+
<!--
4+
This file contains additional configuration written by modules in the NetBeans IDE.
5+
The configuration is intended to be shared among all the users of project and
6+
therefore it is assumed to be part of version control checkout.
7+
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
8+
-->
9+
<spring-data xmlns="http://www.netbeans.org/ns/spring-data/1">
10+
<config-files>
11+
<config-file>src/main/resources/springXMLConfig.xml</config-file>
12+
<config-file>src/main/resources/beans.xml</config-file>
13+
</config-files>
14+
<config-file-groups/>
15+
</spring-data>
16+
</project-shared-configuration>

‎tutorial_beans/pom.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>carmelo.spring</groupId>
5+
<artifactId>tutorial_configuracion</artifactId>
6+
<version>1.0</version>
7+
<packaging>jar</packaging>
8+
9+
<dependencies>
10+
<dependency>
11+
<groupId>org.springframework</groupId>
12+
<artifactId>spring-context</artifactId>
13+
<version>4.3.6.RELEASE</version>
14+
</dependency>
15+
</dependencies>
16+
17+
<properties>
18+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19+
<maven.compiler.source>1.8</maven.compiler.source>
20+
<maven.compiler.target>1.8</maven.compiler.target>
21+
</properties>
22+
23+
</project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package carmelo.spring.introduccion;
2+
3+
public class ClientService {
4+
5+
private static ClientService clientService = new ClientService();
6+
7+
private ClientService() {
8+
}
9+
10+
public static ClientService createInstance() {
11+
return clientService;
12+
}
13+
}
14+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package carmelo.spring.introduccion;
2+
3+
public interface HelloService {
4+
5+
void saludar();
6+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package carmelo.spring.introduccion;
2+
3+
import javax.annotation.PostConstruct;
4+
import javax.annotation.PreDestroy;
5+
import org.springframework.beans.factory.DisposableBean;
6+
import org.springframework.beans.factory.InitializingBean;
7+
import org.springframework.context.annotation.Scope;
8+
import org.springframework.stereotype.Service;
9+
10+
//@Service(value="saludaService")
11+
//@Scope("prototype")
12+
public class HelloServiceImpl implements HelloService/*, InitializingBean, DisposableBean*/ {
13+
14+
@Override
15+
public void saludar() {
16+
System.out.println("--- Hello Spring Framework ---");
17+
}
18+
19+
@PostConstruct
20+
public void initHello() {
21+
System.out.println("--- run init method ---");
22+
}
23+
24+
@PreDestroy
25+
public void destroyHello() {
26+
System.out.println("--- run destroy method ---");
27+
}
28+
29+
// @Override
30+
// public void afterPropertiesSet() throws Exception {
31+
// System.out.println("--- run init method ---");
32+
// }
33+
//
34+
// @Override
35+
// public void destroy() throws Exception {
36+
// System.out.println("--- run destroy method ---");
37+
// }
38+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package carmelo.spring.introduccion;
2+
3+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
4+
import org.springframework.context.support.AbstractApplicationContext;
5+
import org.springframework.context.support.ClassPathXmlApplicationContext;
6+
7+
public class Introduccion {
8+
9+
public static void main(String[] args) {
10+
11+
// configuracion con archivo XML
12+
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("springXMLConfig.xml");
13+
14+
// configuracion con codigo java
15+
// AbstractApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfiguration.class);
16+
17+
for (String bean_name : ctx.getBeanDefinitionNames()) {
18+
System.out.println(":: " + bean_name);
19+
}
20+
21+
// HelloService saluda = ctx.getBean("saludaService", HelloService.class);
22+
// saluda.saludar();
23+
24+
// System.out.println(ctx.getBean("one", ClientService.class));
25+
26+
ctx.close();
27+
}
28+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package carmelo.spring.introduccion;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.context.annotation.Scope;
7+
8+
@Configuration
9+
//@ComponentScan
10+
public class SpringConfiguration {
11+
12+
@Bean("saludaService")
13+
// @Scope("prototype")
14+
public HelloService getSaludaService(){
15+
return new HelloServiceImpl();
16+
}
17+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!-- <?xml version="1.0" encoding="UTF-8"?> -->
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:context="http://www.springframework.org/schema/context"
5+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
6+
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
7+
8+
<!-- <context:component-scan base-package="carmelo.spring.introduccion"/> -->
9+
10+
<bean id="saludaService"
11+
init-method="initHello"
12+
destroy-method="destroyHello"
13+
class="carmelo.spring.introduccion.HelloServiceImpl"/>
14+
15+
<bean id="one"
16+
class="carmelo.spring.introduccion.ClientService"
17+
factory-method="createInstance"/>
18+
19+
</beans>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /