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 ebbc159

Browse files
Spring Inyección automática con autowiring
Habilitar el autowiring en Spring con XML y mendiante anotaciones.
1 parent f28fafa commit ebbc159

File tree

9 files changed

+158
-0
lines changed

9 files changed

+158
-0
lines changed
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_autowiring/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_autowiring</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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package carmelo.spring.autowiring;
2+
3+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
4+
import org.springframework.context.support.AbstractApplicationContext;
5+
import org.springframework.context.support.ClassPathXmlApplicationContext;
6+
7+
/**
8+
*
9+
* @author Carmelo Marin Abrego
10+
*/
11+
public class Autowiring {
12+
13+
/**
14+
* @param args the command line arguments
15+
*/
16+
public static void main(String[] args) {
17+
18+
//AbstractApplicationContext ctx = new AnnotationConfigApplicationContext(SpringJavaConfig.class);
19+
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("springXMLConfig.xml");
20+
21+
for (String bean_name : ctx.getBeanDefinitionNames()) {
22+
System.out.println(":: " + bean_name);
23+
}
24+
25+
EmpleadoService es = ctx.getBean("empleadoService", EmpleadoService.class);
26+
27+
System.out.println("EDao: " + ((EmpleadoServiceImpl)es).getEmpleadoDao());
28+
29+
ctx.close();
30+
}
31+
32+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package carmelo.spring.autowiring;
2+
3+
/**
4+
*
5+
* @author Carmelo Marin Abrego
6+
*/
7+
public interface EmpleadoDao {
8+
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package carmelo.spring.autowiring;
2+
3+
public class EmpleadoDaoImpl implements EmpleadoDao {
4+
5+
@Override
6+
public String toString() {
7+
return "Logica de acceso a datos...";
8+
}
9+
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package carmelo.spring.autowiring;
2+
3+
/**
4+
*
5+
* @author Carmelo Marin Abrego
6+
*/
7+
public interface EmpleadoService {
8+
9+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package carmelo.spring.autowiring;
2+
3+
public class EmpleadoServiceImpl implements EmpleadoService {
4+
5+
private EmpleadoDao empleadoDao;
6+
7+
public EmpleadoServiceImpl() {
8+
9+
}
10+
11+
public EmpleadoServiceImpl(EmpleadoDao empleadoDao) {
12+
this.empleadoDao = empleadoDao;
13+
}
14+
15+
public void setEmpleadoDao(EmpleadoDao empleadoDao) {
16+
this.empleadoDao = empleadoDao;
17+
}
18+
19+
public EmpleadoDao getEmpleadoDao() {
20+
return empleadoDao;
21+
}
22+
23+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package carmelo.spring.autowiring;
2+
3+
import org.springframework.beans.factory.annotation.Autowire;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
7+
//@Configuration
8+
public class SpringJavaConfig {
9+
10+
@Bean
11+
public EmpleadoDao empleadoDao() {
12+
return new EmpleadoDaoImpl();
13+
}
14+
15+
@Bean(autowire = Autowire.BY_TYPE)
16+
public EmpleadoService empleadoService() {
17+
return new EmpleadoServiceImpl();
18+
}
19+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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.autowiring"/> -->
9+
10+
<bean id="empleadoDao"
11+
class="carmelo.spring.autowiring.EmpleadoDaoImpl" />
12+
13+
<bean id="empleadoService"
14+
class="carmelo.spring.autowiring.EmpleadoServiceImpl"
15+
autowire="constructor" />
16+
17+
</beans>

0 commit comments

Comments
(0)

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