Commit for Different Types of Bean Injection in Spring — Draft article
This commit is contained in:
parent
0d85d1ad01
commit
7e9794256d
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.beaninjectiontypes;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class Boat {
|
||||
|
||||
private Engine engine;
|
||||
|
||||
@Autowired
|
||||
public void setEngine(Engine engine) {
|
||||
this.engine = engine;
|
||||
}
|
||||
|
||||
public Engine getEngine() {
|
||||
return engine;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.beaninjectiontypes;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class Car {
|
||||
@Autowired
|
||||
private Engine engine;
|
||||
|
||||
public void setEngine(Engine engine) {
|
||||
this.engine = engine;
|
||||
}
|
||||
|
||||
public Engine getEngine() {
|
||||
return engine;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.beaninjectiontypes;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("com.baeldung.beaninjectiontypes")
|
||||
public class Config {
|
||||
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.beaninjectiontypes;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class Engine {
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.beaninjectiontypes;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class LawnMower {
|
||||
|
||||
@Autowired
|
||||
private Engine engine;
|
||||
|
||||
public LawnMower(Engine engine) {
|
||||
this.engine = engine;
|
||||
}
|
||||
|
||||
public Engine getEngine() {
|
||||
return engine;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.beaninjectiontypes;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class Rocket {
|
||||
|
||||
private Engine engine;
|
||||
|
||||
@Autowired
|
||||
public Rocket(Engine engine) {
|
||||
this.engine = engine;
|
||||
}
|
||||
|
||||
public Engine getEngine() {
|
||||
return engine;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.beaninjectiontypes;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = Config.class)
|
||||
public class BoatTest {
|
||||
|
||||
@Autowired
|
||||
Boat boat;
|
||||
|
||||
@Test
|
||||
public void engineInjectionTest() {
|
||||
assertNotNull(boat);
|
||||
assertNotNull(boat.getEngine());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.beaninjectiontypes;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = Config.class)
|
||||
public class CarTest {
|
||||
|
||||
@Autowired
|
||||
Car car;
|
||||
|
||||
@Test
|
||||
public void engineInjectionTest() {
|
||||
assertNotNull(car);
|
||||
assertNotNull(car.getEngine());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.beaninjectiontypes;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = Config.class)
|
||||
public class LawnMowerTest {
|
||||
|
||||
@Autowired
|
||||
LawnMower lawnMower;
|
||||
|
||||
@Test
|
||||
public void engineInjectionTest() {
|
||||
assertNotNull(lawnMower);
|
||||
assertNotNull(lawnMower.getEngine());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.beaninjectiontypes;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = Config.class)
|
||||
public class RocketTest {
|
||||
|
||||
@Autowired
|
||||
Rocket rocket;
|
||||
|
||||
@Test
|
||||
public void engineInjectionTest() {
|
||||
assertNotNull(rocket);
|
||||
assertNotNull(rocket.getEngine());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue