添加 Mavan 依赖
pom.xml1 2 3 4 5 6
| <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.3.RELEASE</version> </dependency>
|
控制反转及依赖注入
javabean 类
Hello.java1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| public class Hello { private int age; private String msg; private List list; private Set set; private Map<String,Object> map; private Properties properties; private Dog dog; public Hello() { } public Hello(int age) { this.age=age; } public Hello(int age, String msg){ this.msg=msg; this.age=age; } public void say(){ System.out.println("-----------"+msg); } }
|
省略Dog.java…
xml 配置文件
beans_factory.xml1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="method1" class="net.crazykid.bean.Hello"> <constructor-arg index="0" value="3"/> <constructor-arg index="1" value="mike"/> </bean>
<bean id="method2" class="net.crazykid.bean.Hello">
<property name="age" value="3"/> <property name="msg" value="mike"/>
<property name="list"> <list> <value>23</value> <value>fang</value> <value>true</value> </list> </property>
<property name="map"> <map> <entry key="key1" value="abc"></entry> <entry key="key2"> <value>ddd</value> </entry> </map> </property>
<property name="properties"> <props> <prop key="prop1" > 123 </prop> <prop key="prop2"> 2222 </prop> </props> </property>
<property name="dog" ref="dog"/> </bean> <bean id="dog" class="net.crazykid.bean.Dog"> <property name="name" value="小狗" /> </bean>
</beans>
|
注:之后的 XML 配置文件省略头尾标签。
测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public class testHello { private ApplicationContext context = new ClassPathXmlApplicationContext("beans_factory.xml");
@Test public void testIoc(){ Hello hello = (Hello) context.getBean("method2"); hello.say(); } }
|
自定义 bean 工厂…
bean类
Product.java1 2 3 4 5 6 7
| public class Product { private String name; public Product(String name) { this.name = name; } }
|
ProducteFactory1 2 3 4 5 6 7 8
| public class ProducteFactory { public static Product createProduct(){ return new Product("产品1"); } public Product createProduct2(){ return new Product("产品2"); } }
|
xml配置文件
beans_factory.xml1 2 3 4 5 6
| <bean id="product1" class="net.crazykid.bean.ProducteFactory" factory-method="createProduct"/>
<bean id="factory" class="net.crazykid.bean.ProducteFactory" scope="singleton"/> <bean id="product2" factory-bean="factory" factory-method="createProduct2"/>
|
测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public class testFactory { private ClassPathXmlApplicationContext context;
@Before public void init() { context = new ClassPathXmlApplicationContext("beans_factory.xml"); }
@Test public void test01() { Product product1 = (Product) context.getBean("product1"); Product product2 = (Product) context.getBean("product2");
System.out.println(product1); System.out.println(product2); } }
|
i18n国际化
xml配置文件
beans_factory.xml1 2 3 4 5 6 7 8 9
| <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="defaultEncoding" value="UTF-8"/> <property name="basename" value="MessageResource"/> </bean>
|
properties 语言文件
MessageResource_en.properties1 2
| hello=hello hello2=hello {0} welcome to {1}
|
MessageResource_zh.properties1 2
| hello=你好 hello2=你好 {0} 欢迎来到 {1}
|
测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| public class i18nTest { private ClassPathXmlApplicationContext context;
@Before public void init() { context = new ClassPathXmlApplicationContext("beans_factory.xml"); }
@Test public void Test1() { ResourceBundleMessageSource source = (ResourceBundleMessageSource) context.getBean("messageSource"); String msg = source.getMessage("hello", null, new Locale("en", "US")); System.out.println("信息:" + msg);
String msg2 = source.getMessage("hello", null, new Locale("zh", "CN")); System.out.println("信息:" + msg2); context.close(); }
@Test public void Test2() { ResourceBundleMessageSource source = (ResourceBundleMessageSource) context.getBean("messageSource"); String msg = source.getMessage("hello2", new Object[] {"zhangsan","gz"}, new Locale("en", "US")); System.out.println("信息:" + msg);
String msg2 = source.getMessage("hello2", new Object[] {"张三","广州"}, new Locale("zh", "CN")); System.out.println("信息:" + msg2); context.close(); } }
|
监听器(事件)
XML配置文件
beans_listener.xml1
| <bean id="appListener" class="net.crazykid.bean.AppListener"></bean>
|
bean类
AppListener.java1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public class AppListener implements ApplicationListener { @Override public void onApplicationEvent(ApplicationEvent event) { if (event instanceof ContextRefreshedEvent) { System.out.println("ContextRefreshedEvent事件被触发了"); } else if (event instanceof ContextStartedEvent) { System.out.println("ContextStartedEvent事件被触发了"); } else if (event instanceof ContextStoppedEvent) { System.out.println("ContextStoppedEvent事件被触发了"); } else if (event instanceof ContextClosedEvent) { System.out.println("ContextClosedEvent事件被触发了"); } } }
|
测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public class ListenerText { private ClassPathXmlApplicationContext context;
@Before public void init() { context = new ClassPathXmlApplicationContext("beans_listener.xml"); }
@Test public void test01() { context.refresh(); context.start(); context.stop(); context.close(); } }
|
配置数据源
Mavan 引入c3p0
pom.xml1 2 3 4 5 6
| <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency>
|
数据源配置文件 properties
db.properties1 2 3 4
| jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/store jdbc.username=root jdbc.password=123456
|
XML配置文件
这里踩了坑:classpath:
后面不能有空格!!!!!否则报错!!!!
beans_properties.xml1 2 3 4 5 6 7 8 9
| <context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClassName}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean>
|
测试类
1 2 3 4 5 6 7 8 9
| public class testBeansProperties { private ApplicationContext context = new ClassPathXmlApplicationContext("beans_properties.xml");
@Test public void test01() { Object dataSource = context.getBean("dataSource"); System.out.println(dataSource); } }
|
注解
XML配置文件
beans_zhujie.xml1 2 3 4 5 6 7 8
| <context:annotation-config/>
<context:component-scan base-package="net.crazykid.bean"/>
|
bean类
Monkey.java1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| @Component()
public class Monkey{ private String name; private MonkeyDaddy monkeyDaddy;
public Monkey() { }
public Monkey(String name) { this.name = name; }
public String getName() { return name; }
@Value("猴子") public void setName(String name) { this.name = name; }
public MonkeyDaddy getMonkeyDaddy() { return monkeyDaddy; }
@Autowired @Qualifier("monkeyDaddy") public void setMonkeyDaddy(MonkeyDaddy monkeyDaddy) { this.monkeyDaddy = monkeyDaddy; }
@PostConstruct public void OnConstruct() { System.out.println("Monkey类被构造了"); }
@PreDestroy public void OnDestroy() { System.out.println("Monkey类被销毁了"); }
@Override public String toString() { return "Monkey{" + "name='" + name + '\'' + ", monkeyDaddy=" + monkeyDaddy + '}'; } }
|
MonkeyDaddy.java1 2 3 4 5 6 7 8 9 10 11
| @Component() @Primary public class MonkeyDaddy{ private String name; @Value("猴子的爸爸") public void setName(String name) { this.name = name; } }
|
测试类
1 2 3 4 5 6 7 8 9 10 11 12 13
| public class testZhujie { private ClassPathXmlApplicationContext context;
@Before public void init() { context = new ClassPathXmlApplicationContext("beans_zhujie.xml"); }
@Test public void test01() { System.out.println(context.getBean("monkey")); } }
|
输出结果:
Monkey类被构造了
Monkey{name=’猴子’, monkeyDaddy=MonkeyDaddy{name=’猴子的爸爸’}}