spring初识

spring框架的使用我们不需要创建对象,通过配置javabean的方式来获取对象;

1.设计一个Javabean,Javabean设计原型有四点,(1)公有类的(2)无参的构造方法(3)属性私有(4)要有get和set方法

2.然后配置bean.xml

<?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-3.0.xsd">

   <bean id="helloWorld" class="com.tutorialspoint.HelloWorld">
       <property name="message" value="Hello World!"/>//通过注入的方式给message属性赋值为HelloWorld

</bean> </beans>

3.配置beans加载到程序当中

ApplicationContext context = 
 new ClassPathXmlApplicationContext("Beans.xml");

5.创建我们的对象

HelloWorld obj = (HelloWorld) context.getBean("helloWorld");

然后就可以通过该对象调用他的方法、、、、、、真是麻烦直接new不就行啦

原文地址:https://www.cnblogs.com/cc233/p/6912126.html