Last active
October 2, 2015 18:58
-
-
Save HiroNakamura/2301420 to your computer and use it in GitHub Desktop.
Programando en Spring Framework 002
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//1 | |
package com.pruebaspring.pruebaspring2; | |
import org.springframework.context.ApplicationContext; | |
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.ComponentScan; | |
import org.springframework.context.annotation.Configuration; | |
@Configuration | |
@ComponentScan | |
public class Main { | |
@Bean | |
GradosMetodos mockGradosMetodos(){ | |
return new GradosMetodos(){ | |
public double obtenerConversion(double g){ | |
return (1.8*g)+32; | |
} | |
}; | |
} | |
public static void main(String[] args){ | |
ApplicationContext context = new AnnotationConfigApplicationContext(Main.class); | |
GradosMetodos gradosMetodos =context.getBean(GradosMetodos.class); | |
System.out.println("Conversion obtenida: "+gradosMetodos.obtenerConversion(9)); | |
} | |
} | |
//2 | |
package com.pruebaspring.pruebaspring2; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Component; | |
@Component | |
public class GradosMetodosImpl{ | |
@Autowired | |
private GradosMetodos gradosMetodos; | |
private double grados; | |
public void imprime(){ | |
System.out.println(this.gradosMetodos.obtenerConversion(this.grados)); | |
} | |
public void setGrados(double grados) { | |
this.grados = grados; | |
} | |
public double getGrados() { | |
return grados; | |
} | |
} | |
//3 | |
package com.pruebaspring.pruebaspring2; | |
public interface GradosMetodos { | |
public abstract double obtenerConversion(double g); | |
} | |
---------------------------------------------------- | |
context.xml | |
<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" | |
"http://www.springframework.org/dtd/spring-beans.dtd"> | |
<beans> | |
<bean id="gradosMetodos" | |
class="org.springpruebas.prueba02.GradosMetodosImpl"> | |
</bean> | |
</beans> | |
GradosMetodos.java | |
package org.springpruebas.prueba02; | |
/** | |
* | |
* @author | |
*/ | |
public interface GradosMetodos { | |
public abstract double obtenerConversion(double g); | |
} | |
GradosMetodosImpl.java | |
package org.springpruebas.prueba02; | |
/** | |
* | |
* @author | |
*/ | |
public class GradosMetodosImpl implements GradosMetodos { | |
private double g; | |
public double obtenerConversion(double g) { | |
//F=(1.8*C)+32; | |
return (1.8*g)+32; | |
} | |
/** | |
* @return the valor | |
*/ | |
/** | |
* @param valor the valor to set | |
*/ | |
public void setValor(double g) { | |
this.g = g; | |
} | |
} | |
Main.java | |
package org.springpruebas.prueba02; | |
import org.springframework.context.ApplicationContext; | |
import org.springframework.context.support.ClassPathXmlApplicationContext; | |
/** | |
* | |
* @author | |
*/ | |
public class Main { | |
public static void main(String[] args) throws Exception { | |
ApplicationContext application= new ClassPathXmlApplicationContext("context.xml"); | |
GradosMetodos gradosMetodos =(GradosMetodos) application.getBean("gradosMetodos"); | |
System.out.println(gradosMetodos.obtenerConversion(9)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
//1
package com.pruebaspring.pruebaspring2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@configuration
@componentscan
public class Main {
}
//2
package com.pruebaspring.pruebaspring2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@component
public class GradosMetodosImpl{
@Autowired
private GradosMetodos gradosMetodos;
private double grados;
}
//3
package com.pruebaspring.pruebaspring2;
public interface GradosMetodos {
public abstract double obtenerConversion(double g);
}
//4 pom.xml
4.0.0
com.pruebaSpring
pruebaSpring
1.0-SNAPSHOT
jar
pruebaSpring
UTF-8http://maven.apache.org