1打开idea----new project
2点击建立项目的类型maven 点击next
3点击next
4选择路径,点击完成
5建立成功之后修改pom.xml配置文件
添加,解决依赖关系
<dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.3.13.RELEASE</version></dependency>
</dependencies>
6在main下建立一个hello包----下面建立三个类
MesasageService类
package hello;public class MesasageService {/*** 执行打印功能* @return 返回要打印的字符串*/public String getMessage(){return "Hello World";}
}
MessagePrinter类
package hello;public class MessagePrinter {private MesasageService service;/** 简历和MessageService的关系* *///设置service的值public void setService(MesasageService service){this.service =service;}public void printMessage(){System.out.println(this.service.getMessage());}
}
application类
package hello;import javax.print.attribute.standard.RequestingUserName;public class Application {public static void main(String[] args){System.out.println("application" + "");//创建打印机对象MessagePrinter printer = new MessagePrinter();//创建消息服务对象MesasageService service = new MesasageService();//设置打印机属性printer.setService(service);//打印消息printer.printMessage();}
}
运行结果
application
Hello World